fix error handling in entity-service

This commit is contained in:
Pierre Noël 2021-10-28 12:10:22 +02:00
parent b4c9ad0440
commit 70c98f7ed9
2 changed files with 16 additions and 10 deletions

View File

@ -59,16 +59,22 @@ module.exports = ctx => {
// wrap methods to handle Database Errors // wrap methods to handle Database Errors
service.decorate(oldService => { service.decorate(oldService => {
const newService = _.mapValues(oldService, (method, methodName) => async (...args) => { const newService = _.mapValues(
oldService,
(method, methodName) =>
async function(...args) {
try { try {
return await oldService[methodName].call(newService, ...args); return await oldService[methodName].call(this, ...args);
} catch (error) { } catch (error) {
if (databaseErrorsToTransform.some(errorToTransform => error instanceof errorToTransform)) { if (
databaseErrorsToTransform.some(errorToTransform => error instanceof errorToTransform)
) {
throw new ValidationError(error.message); throw new ValidationError(error.message);
} }
throw error; throw error;
} }
}); }
);
return newService; return newService;
}); });

View File

@ -50,7 +50,7 @@ class ForbiddenError extends ApplicationError {
constructor(message, details) { constructor(message, details) {
super(message, details); super(message, details);
this.name = 'ForbiddenError'; this.name = 'ForbiddenError';
this.message = message || 'Forbidden action'; this.message = message || 'Forbidden access';
} }
} }