manager-bh/server/_helpers/error-handler.js

19 lines
567 B
JavaScript
Raw Permalink Normal View History

2024-11-04 18:47:17 +03:00
const { getAnswer } = require('../utils/common');
function errorHandler(err, req, res, _next) {
console.error(err);
if (typeof (err) === 'string') {
return res.status(400).json(getAnswer([{ message: err }]));
}
if (err.name === 'UnauthorizedError') {
// jwt authentication error
return res.status(401).json(getAnswer([{ message: 'Invalid Token' }]));
}
// default to 500 server error
return res.status(500).json(getAnswer([{ message: err?.message || 'Invalid Token' }]));
}
module.exports = errorHandler;