From 386de69bceb91c9fc6adeea666cf2b842fd31c96 Mon Sep 17 00:00:00 2001 From: jorgep Date: Thu, 24 Aug 2023 12:47:10 +0200 Subject: [PATCH] refs #5216 fix error handling --- .../00-addExpeditionState.sql | 0 .../expedition-state/addExpeditionState.js | 25 ++++++++----------- .../specs/addExpeditionState.spec.js | 6 ++--- 3 files changed, 13 insertions(+), 18 deletions(-) rename db/changes/{233201 => 233601}/00-addExpeditionState.sql (100%) diff --git a/db/changes/233201/00-addExpeditionState.sql b/db/changes/233601/00-addExpeditionState.sql similarity index 100% rename from db/changes/233201/00-addExpeditionState.sql rename to db/changes/233601/00-addExpeditionState.sql diff --git a/modules/ticket/back/methods/expedition-state/addExpeditionState.js b/modules/ticket/back/methods/expedition-state/addExpeditionState.js index cc70cbb8d..e82d279b0 100644 --- a/modules/ticket/back/methods/expedition-state/addExpeditionState.js +++ b/modules/ticket/back/methods/expedition-state/addExpeditionState.js @@ -1,3 +1,5 @@ +const UserError = require('vn-loopback/util/user-error'); + module.exports = Self => { Self.remoteMethod('addExpeditionState', { description: 'Update an expedition state', @@ -31,8 +33,7 @@ module.exports = Self => { tx = await Self.beginTransaction({}); myOptions.transaction = tx; } - - const promises = []; + let expeditionId; try { for (const expedition of expeditions) { const expeditionStateType = await models.ExpeditionStateType.findOne( @@ -42,30 +43,24 @@ module.exports = Self => { } ); if (!expeditionStateType) - throw new Error(`The state code: ${expedition.stateCode} does not exist.`); + throw new UserError(`Invalid state code: ${expedition.stateCode}.`); const typeFk = expeditionStateType.id; + expeditionId = expedition.expeditionFk; - const existsExpedition = await models.Expedition.findOne({ - fields: ['id'], - where: {id: expedition.expeditionFk} - }); - if (!existsExpedition) - throw new Error(`The expedition with id: ${expedition.expeditionFk} does not exist.`); - - const newExpeditionState = models.ExpeditionState.create({ + await models.ExpeditionState.create({ expeditionFk: expedition.expeditionFk, typeFk, }); - promises.push(newExpeditionState); } - await Promise.all(promises); - if (tx) await tx.commit(); } catch (e) { if (tx) await tx.rollback(); - throw e; + if (e instanceof UserError) + throw e; + + throw new UserError(`Invalid expedition id: ${expeditionId}.`); } }; }; diff --git a/modules/ticket/back/methods/expedition-state/specs/addExpeditionState.spec.js b/modules/ticket/back/methods/expedition-state/specs/addExpeditionState.spec.js index c9c40ccd9..819a43a60 100644 --- a/modules/ticket/back/methods/expedition-state/specs/addExpeditionState.spec.js +++ b/modules/ticket/back/methods/expedition-state/specs/addExpeditionState.spec.js @@ -8,7 +8,7 @@ describe('expeditionState addExpeditionState()', () => { const options = {transaction: tx}; const payload = [ { - expeditionFk: 5, + expeditionFk: 8, stateCode: 'ON DELIVERY' }, ]; @@ -47,7 +47,7 @@ describe('expeditionState addExpeditionState()', () => { await tx.rollback(); } - expect(error.message).toContain('The state code: DUMMY does not exist.'); + expect(error.message).toContain('Invalid state code: DUMMY.'); }); it('should throw an error when expeditionFk does not exist', async() => { @@ -70,6 +70,6 @@ describe('expeditionState addExpeditionState()', () => { await tx.rollback(); } - expect(error.message).toContain('The expedition with id: 50 does not exist.'); + expect(error.message).toContain('Invalid expedition id: 50.'); }); });