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