2021-05-10 15:47:26 +00:00
|
|
|
const ErrorResponse = require('../utils/ErrorResponse');
|
2021-06-22 12:49:00 +00:00
|
|
|
const Logger = require('../utils/Logger');
|
|
|
|
const logger = new Logger();
|
2021-05-10 15:47:26 +00:00
|
|
|
|
|
|
|
const errorHandler = (err, req, res, next) => {
|
|
|
|
let error = { ...err };
|
|
|
|
error.message = err.message;
|
|
|
|
|
2021-05-22 17:04:34 +00:00
|
|
|
// if (error.errors[0].type === 'unique violation') {
|
|
|
|
// const msg = error.errors[0].message;
|
|
|
|
// error = new ErrorResponse(`Field ${msg}`, 400);
|
|
|
|
// }
|
2021-05-17 16:26:01 +00:00
|
|
|
|
2021-06-22 12:49:00 +00:00
|
|
|
logger.log(error.message.split(',')[0], 'ERROR');
|
2021-05-10 15:47:26 +00:00
|
|
|
|
2021-10-21 22:42:27 +00:00
|
|
|
if (process.env.NODE_ENV == 'development') {
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
|
2021-05-10 15:47:26 +00:00
|
|
|
res.status(err.statusCode || 500).json({
|
|
|
|
success: false,
|
2021-10-21 22:42:27 +00:00
|
|
|
error: error.message || 'Server Error',
|
|
|
|
});
|
|
|
|
};
|
2021-05-10 15:47:26 +00:00
|
|
|
|
2021-10-21 22:42:27 +00:00
|
|
|
module.exports = errorHandler;
|