flame/middleware/errorHandler.js

22 lines
547 B
JavaScript
Raw Normal View History

const ErrorResponse = require('../utils/ErrorResponse');
2021-05-17 19:26:01 +03:00
const colors = require('colors');
const errorHandler = (err, req, res, next) => {
let error = { ...err };
error.message = err.message;
2021-05-17 19:26:01 +03:00
if (error.errors[0].type === 'unique violation') {
const msg = error.errors[0].message;
error = new ErrorResponse(`Field ${msg}`, 400);
}
console.log(error);
console.log(`${err}`.bgRed);
res.status(err.statusCode || 500).json({
success: false,
error: error.message || 'Server Error'
})
}
module.exports = errorHandler;