jws-backend/src/middlewares/error.ts

34 lines
933 B
TypeScript
Raw Normal View History

2024-04-01 13:28:43 +07:00
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) {
if (error instanceof HttpError) {
return res.status(error.status).json({
status: error.status,
message: error.message,
2024-06-14 04:39:48 +00:00
code: error.code,
2024-04-01 13:28:43 +07:00
});
}
if (error instanceof ValidateError) {
return res.status(error.status).json({
status: HttpStatus.UNPROCESSABLE_ENTITY,
message: "Validation error(s).",
detail: error.fields,
2024-06-14 04:39:48 +00:00
code: "validateError",
2024-04-01 13:28:43 +07:00
});
}
console.error("Exception Caught:" + error);
return res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({
status: HttpStatus.INTERNAL_SERVER_ERROR,
message: error.message,
2024-08-29 13:09:44 +07:00
code: "systemError",
2024-04-01 13:28:43 +07:00
});
}
export default error;