salix/loopback/server/middleware/error-handler.js

40 lines
1.2 KiB
JavaScript
Raw Normal View History

const SalixError = require('../../util/salixError');
2018-12-27 11:54:16 +00:00
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 SalixError) {
err.message = req.__(err.message, ...err.translateArgs);
return next(err);
}
// Validation errors
if (err.statusCode == 422) {
try {
let code;
let {messages} = err.details;
for (code in messages) break;
err.message = req.__(messages[code][0]);
return next(err);
} catch (e) {}
}
// MySQL user-defined exceptions
2024-06-26 10:25:18 +00:00
if (err.sqlState == '45000' || err?.errno == 4025)
return next(new UserError(req.__(err.sqlMessage)));
// Logs error to console
let env = process.env.NODE_ENV;
let useCustomLogging = env && env != 'development' &&
(!err.statusCode || err.statusCode >= 500);
if (useCustomLogging)
logToConsole(req, err);
next(err);
};
};