You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

26 lines
672 B

import * as express from "express";
import ApiError from "@common/ApiError";
import { ErrorCode } from "@common/ErrorCode";
const isDev = process.env.NODE_ENV === "development";
const errorHandler: express.ErrorRequestHandler = (
err: any,
req: express.Request,
res: express.Response,
next: express.NextFunction
) => {
if (err instanceof ApiError) {
// TODO: different content-type?
res.status(err.statusCode).json(err.toJSON(isDev));
} else {
const internalError = new ApiError(
"An internal server error has occurred",
ErrorCode.Internal
);
errorHandler(internalError, req, res, next);
}
};
export default errorHandler;