File size: 1,076 Bytes
4e83772 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import { Request, Response, NextFunction } from "express";
import { ENVS } from "@/config/env.config.js";
import AppError from "@/utils/AppError.js";
import { COMMON_ERRORS } from "@/constants/errors.js";
export const errorHandler = (err: any, req: Request, res: Response, next: NextFunction) => {
let statusCode = err.statusCode || 500;
let message = err.message || COMMON_ERRORS.INTERNAL_SERVER;
console.log("New error 🎉🎉🎉🎉🎉");
if (err.cause && !(err instanceof AppError)) {
console.error("Cause: ", err.cause, "\n----------------------------------------------------------------");
}
console.log(err);
if (!(err instanceof AppError)) {
if (err.code === 11000) {
const field = Object.keys(err.keyValue)[0];
message = `${field} already exists`;
statusCode = 400;
} else {
console.error("Unhandled Error:", err);
message = COMMON_ERRORS.SOMETHING_WENT_WRONG;
}
}
const errorResponse = {
message,
statusCode,
...(ENVS.NODE_ENV === "development" && { stack: err.stack }),
};
res.status(statusCode).json(errorResponse);
};
|