-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrorHandler.js
More file actions
22 lines (18 loc) · 760 Bytes
/
Copy patherrorHandler.js
File metadata and controls
22 lines (18 loc) · 760 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// middleware/errorHandler.js
// Centralized Express error handler. Any error thrown (or passed to next())
// anywhere in the app ends up here, giving a consistent JSON error response.
const notFound = (req, res, next) => {
const error = new Error(`Route not found - ${req.originalUrl}`);
res.status(404);
next(error);
};
const errorHandler = (err, req, res, next) => {
// If a status code was already set (e.g. 401, 403) keep it, otherwise default to 500
const statusCode = res.statusCode && res.statusCode !== 200 ? res.statusCode : 500;
res.status(statusCode).json({
success: false,
message: err.message,
stack: process.env.NODE_ENV === 'production' ? undefined : err.stack,
});
};
module.exports = { notFound, errorHandler };