From 70c98f7ed9c5eebd26bb783165a05e1371c48e19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pierre=20No=C3=ABl?= Date: Thu, 28 Oct 2021 12:10:22 +0200 Subject: [PATCH] fix error handling in entity-service --- .../lib/services/entity-service/index.js | 24 ++++++++++++------- packages/core/utils/lib/errors.js | 2 +- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/packages/core/strapi/lib/services/entity-service/index.js b/packages/core/strapi/lib/services/entity-service/index.js index eef11bc8d4..98a2d16476 100644 --- a/packages/core/strapi/lib/services/entity-service/index.js +++ b/packages/core/strapi/lib/services/entity-service/index.js @@ -59,16 +59,22 @@ module.exports = ctx => { // wrap methods to handle Database Errors service.decorate(oldService => { - const newService = _.mapValues(oldService, (method, methodName) => async (...args) => { - try { - return await oldService[methodName].call(newService, ...args); - } catch (error) { - if (databaseErrorsToTransform.some(errorToTransform => error instanceof errorToTransform)) { - throw new ValidationError(error.message); + const newService = _.mapValues( + oldService, + (method, methodName) => + async function(...args) { + try { + return await oldService[methodName].call(this, ...args); + } catch (error) { + if ( + databaseErrorsToTransform.some(errorToTransform => error instanceof errorToTransform) + ) { + throw new ValidationError(error.message); + } + throw error; + } } - throw error; - } - }); + ); return newService; }); diff --git a/packages/core/utils/lib/errors.js b/packages/core/utils/lib/errors.js index 06683fd965..4c758a23e4 100644 --- a/packages/core/utils/lib/errors.js +++ b/packages/core/utils/lib/errors.js @@ -50,7 +50,7 @@ class ForbiddenError extends ApplicationError { constructor(message, details) { super(message, details); this.name = 'ForbiddenError'; - this.message = message || 'Forbidden action'; + this.message = message || 'Forbidden access'; } }