Files
Wegwichtel/src/middleware/errors.js
T
2026-06-16 10:31:41 +02:00

26 lines
833 B
JavaScript

export class HttpError extends Error {
constructor(status, message, details = undefined) {
super(message);
this.name = 'HttpError';
this.status = status;
this.details = details;
}
}
export function notFoundHandler(req, res, next) {
next(new HttpError(404, 'Ressource nicht gefunden.'));
}
export function errorHandler(error, req, res, next) {
if (res.headersSent) return next(error);
let status = Number.isInteger(error.status) ? error.status : 500;
if (error.code === 'LIMIT_FILE_SIZE') status = 413;
if (error.code === 'LIMIT_UNEXPECTED_FILE') status = 400;
if (status >= 500) console.error(error);
res.status(status).json({
error: error.name || 'Error',
message: status >= 500 ? 'Interner Serverfehler.' : error.message,
...(error.details ? { details: error.details } : {})
});
}