start project

This commit is contained in:
Warunee Tamkoo 2024-09-05 13:59:43 +07:00
commit 0703810fa3
62 changed files with 12665 additions and 0 deletions

36
src/middlewares/error.ts Normal file
View file

@ -0,0 +1,36 @@
import { NextFunction, Request, Response } from "express";
import HttpError from "../interfaces/http-error";
import HttpStatus from "../interfaces/http-status";
import { ValidateError } from "tsoa";
function error(error: Error, _req: Request, res: Response, _next: NextFunction) {
const logData = _req.app.locals.logData.sequence?.at(-1);
if (logData) {
logData.status = "error";
logData.description = error.message;
}
if (error instanceof HttpError) {
return res.status(error.status).json({
status: error.status,
message: error.message,
});
}
if (error instanceof ValidateError) {
return res.status(error.status).json({
status: HttpStatus.UNPROCESSABLE_ENTITY,
message: "Validation error(s).",
detail: error.fields,
});
}
console.error("Exception Caught:" + error);
return res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({
status: HttpStatus.INTERNAL_SERVER_ERROR,
message: error.message,
});
}
export default error;