33 lines
972 B
JavaScript
33 lines
972 B
JavaScript
const UserError = require('../../util/user-error');
|
|
const logToConsole = require('strong-error-handler/lib/logger');
|
|
|
|
module.exports = function() {
|
|
return function(err, req, res, next) {
|
|
// Thrown user errors
|
|
if (err instanceof UserError) {
|
|
err.message = req.__(err.message, ...err.translateArgs);
|
|
return next(err);
|
|
}
|
|
|
|
// Validation errors
|
|
if (err.statusCode == 422) {
|
|
try {
|
|
let code;
|
|
let messages = err.details.messages;
|
|
for (code in messages) break;
|
|
err.message = req.__(messages[code][0]);
|
|
return next(err);
|
|
} catch (e) {}
|
|
}
|
|
|
|
// MySQL user-defined exceptions
|
|
if (err.sqlState == '45000')
|
|
return next(new UserError(req.__(err.sqlMessage)));
|
|
|
|
if (!err.statusCode || err.statusCode >= 500)
|
|
logToConsole(req, err);
|
|
|
|
next(err);
|
|
};
|
|
};
|