From 33da9c7fec1c41d0efdd95ed42d611e3c0108209 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Tue, 2 Jan 2024 11:59:35 +0100 Subject: [PATCH] refs #6172 perf: translate message error --- loopback/server/middleware/error-handler.js | 3 ++- loopback/util/forbiddenError.js | 4 +++- loopback/util/salixError.js | 5 +++++ loopback/util/user-error.js | 4 +++- 4 files changed, 13 insertions(+), 3 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..66e75c47f 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); } diff --git a/loopback/util/forbiddenError.js b/loopback/util/forbiddenError.js index 998cb4593..261f89f06 100644 --- a/loopback/util/forbiddenError.js +++ b/loopback/util/forbiddenError.js @@ -1,4 +1,6 @@ -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'; 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..6ba851ba8 100644 --- a/loopback/util/user-error.js +++ b/loopback/util/user-error.js @@ -4,7 +4,9 @@ * 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';