File size: 1,055 Bytes
964569f | 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 33 34 35 36 37 38 39 40 41 | const { AppError } = require("../utils/appError");
function createNotFoundMiddleware() {
return (req, res) => {
res.status(404).json({
message: "Route not found",
path: req.originalUrl,
});
};
}
function createErrorHandler(config, logger) {
return (error, req, res, _next) => {
const appError =
error instanceof AppError
? error
: new AppError("Internal server error", 500);
logger.error("Unhandled API error", {
requestId: req.context && req.context.requestId,
path: req.originalUrl,
method: req.method,
statusCode: appError.statusCode,
message: error.message,
stack: config.nodeEnv === "production" ? undefined : error.stack,
});
res.status(appError.statusCode).json({
message: appError.message,
requestId: req.context && req.context.requestId,
details:
config.nodeEnv === "production" ? undefined : appError.details || error.message,
});
};
}
module.exports = {
createNotFoundMiddleware,
createErrorHandler,
};
|