From cc0c8443bf0c4ff938a6f9a14ed44a3ade04812c Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Wed, 3 Jan 2024 08:06:20 +0100 Subject: [PATCH] refs #6613 perf: translate message error --- loopback/server/middleware/error-handler.js | 5 +++-- loopback/util/forbiddenError.js | 5 +++-- loopback/util/salixError.js | 5 +++++ loopback/util/user-error.js | 5 +++-- 4 files changed, 14 insertions(+), 6 deletions(-) create mode 100644 loopback/util/salixError.js diff --git a/loopback/server/middleware/error-handler.js b/loopback/server/middleware/error-handler.js index 725826ae7..cc7b81618 100644 --- a/loopback/server/middleware/error-handler.js +++ b/loopback/server/middleware/error-handler.js @@ -1,10 +1,11 @@ +const SalixError = require('../../util/salixError'); 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) { + if (err instanceof SalixError) { err.message = req.__(err.message, ...err.translateArgs); return next(err); } @@ -13,7 +14,7 @@ module.exports = function() { if (err.statusCode == 422) { try { let code; - let messages = err.details.messages; + let {messages} = err.details; for (code in messages) break; err.message = req.__(messages[code][0]); return next(err); diff --git a/loopback/util/forbiddenError.js b/loopback/util/forbiddenError.js index 998cb4593..8f6561059 100644 --- a/loopback/util/forbiddenError.js +++ b/loopback/util/forbiddenError.js @@ -1,7 +1,8 @@ -module.exports = class ForbiddenError extends Error { +const SalixError = require('./salixError'); +module.exports = class ForbiddenError extends SalixError { constructor(message, code, ...translateArgs) { super(message); - this.name = 'ForbiddenError'; + this.name = ForbiddenError.name; this.statusCode = 403; this.code = code; this.translateArgs = translateArgs; diff --git a/loopback/util/salixError.js b/loopback/util/salixError.js new file mode 100644 index 000000000..427b871ab --- /dev/null +++ b/loopback/util/salixError.js @@ -0,0 +1,5 @@ +module.exports = class SalixError extends Error { + constructor(message) { + super(message); + } +}; diff --git a/loopback/util/user-error.js b/loopback/util/user-error.js index c2d01e080..feee208b3 100644 --- a/loopback/util/user-error.js +++ b/loopback/util/user-error.js @@ -4,10 +4,11 @@ * the final user, so they cannot contain sensitive data and must * be understandable by people who do not have a technical profile. */ -module.exports = class UserError extends Error { +const SalixError = require('./salixError'); +module.exports = class UserError extends SalixError { constructor(message, code, ...translateArgs) { super(message); - this.name = 'UserError'; + this.name = UserError.name; this.statusCode = 400; this.code = code; this.translateArgs = translateArgs;