-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrorHandler.js
More file actions
42 lines (35 loc) · 871 Bytes
/
errorHandler.js
File metadata and controls
42 lines (35 loc) · 871 Bytes
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
42
/**
* Error handling middleware
*/
const winston = require('winston');
const logger = require('../utils/logger');
/**
* Central error handling middleware
*/
const errorHandler = (err, req, res, next) => {
// Log the error
logger.error({
message: err.message,
stack: err.stack,
path: req.path,
method: req.method,
ip: req.ip
});
// Determine status code
const statusCode = err.statusCode || 500;
// Prepare error response
const errorResponse = {
success: false,
error: {
message: err.message || 'Internal Server Error',
code: err.code || 'INTERNAL_ERROR'
}
};
// Add stack trace in development mode
if (process.env.NODE_ENV === 'development') {
errorResponse.error.stack = err.stack;
}
// Send error response
res.status(statusCode).json(errorResponse);
};
module.exports = errorHandler;