35 lines
1.3 KiB
JavaScript
35 lines
1.3 KiB
JavaScript
"use strict";
|
|
/**
|
|
* Centralized Express error handler — production-safe messages and consistent status codes.
|
|
*/
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.asyncHandler = asyncHandler;
|
|
exports.errorHandler = errorHandler;
|
|
const safeError_1 = require("../utils/safeError");
|
|
const httpError_1 = require("../utils/httpError");
|
|
const sentry_1 = require("../observability/sentry");
|
|
/**
|
|
* Wraps an async route handler so rejections reach the global error middleware.
|
|
*/
|
|
function asyncHandler(fn) {
|
|
return (req, res, next) => {
|
|
void fn(req, res, next).catch(next);
|
|
};
|
|
}
|
|
function errorHandler(err, _req, res, _next) {
|
|
if (res.headersSent)
|
|
return;
|
|
const status = (0, httpError_1.httpStatusForError)(err);
|
|
const code = err?.code ?? (err instanceof httpError_1.HttpError ? err.code : undefined);
|
|
if (status >= 500) {
|
|
console.error(err);
|
|
(0, sentry_1.captureException)(err);
|
|
}
|
|
const fallback = status >= 500 ? 'Internal server error' : 'Request failed';
|
|
const expose = err instanceof httpError_1.HttpError ? err.expose : status < 500;
|
|
res.status(status).json({
|
|
error: expose ? (0, safeError_1.clientErrorMessage)(err, fallback) : fallback,
|
|
...(code ? { code } : {}),
|
|
});
|
|
}
|
|
//# sourceMappingURL=errorHandler.js.map
|