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
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;
});

View File

@ -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';
}
}