manager-bh/server/_helpers/error-handler.js
Primakov Alexandr Alexandrovich ef35a8aa6c init
2024-11-04 18:47:17 +03:00

19 lines
567 B
JavaScript

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;