From 8a3f9c34786cf2edc7119a798b94434044c0c9ae Mon Sep 17 00:00:00 2001 From: Crash-- Date: Thu, 25 Jan 2024 13:32:33 +0100 Subject: [PATCH] fix: Don't crash service if there is no database Now, each errors are thrown... So if there is no database because there is no bills for instance, the service was crashing. So I took this commit https://github.com/cozy/cozy-banks/commit/7a8a8a33ad353a59d88334076d54bcf9cf80be6c and test if the error is because there is no database before throwing. --- .../services/onOperationOrBillCreate.js | 21 ++++++++++++++----- .../onOperationOrBillCreateHelpers.js | 16 ++++++++++---- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/targets/services/onOperationOrBillCreate.js b/src/targets/services/onOperationOrBillCreate.js index 9d10f854d8..a5e406e2f5 100644 --- a/src/targets/services/onOperationOrBillCreate.js +++ b/src/targets/services/onOperationOrBillCreate.js @@ -34,11 +34,22 @@ const onOperationOrBillCreate = async (client, options) => { // may be missed by `TransactionGreater` because their `_rev` don't start with `1` const notifLastSeq = setting.notifications.lastSeq log('info', '⌛ Fetching transaction changes...') - const notifChanges = await fetchChangesOrAll( - client, - TRANSACTION_DOCTYPE, - notifLastSeq - ) + let notifChanges + try { + notifChanges = await fetchChangesOrAll( + client, + TRANSACTION_DOCTYPE, + notifLastSeq + ) + } catch (e) { + if (!/Database does not exist/.test(e)) { + throw e + } else { + log('info', 'No transaction database so early exit') + process.exit(0) + } + } + log('info', '✅ Transaction changes fetched') const brands = await makeBrands(client, undefined, true) diff --git a/src/targets/services/onOperationOrBillCreateHelpers.js b/src/targets/services/onOperationOrBillCreateHelpers.js index 66a7858472..2a2ffaa5d3 100644 --- a/src/targets/services/onOperationOrBillCreateHelpers.js +++ b/src/targets/services/onOperationOrBillCreateHelpers.js @@ -56,8 +56,12 @@ export const doBillsMatching = async ( logResult(result) } } catch (e) { - log('error', `❗ [Bills matching service] ${e}`) - throw e + if (!/Database does not exist/.test(e)) { + log('error', `❗ [Bills matching service] ${e}`) + throw e + } else { + log('info', `⚠️ [Bills matching no database]`) + } } } @@ -104,8 +108,12 @@ export const doTransactionsMatching = async ( logResult(result) } } catch (e) { - log('error', `❗ [Transactions matching service] ${e}`) - throw e + if (!/Database does not exist/.test(e)) { + log('error', `❗ [Transactions matching service] ${e}`) + throw e + } else { + log('info', `⚠️ [Transactions matching service no database]`) + } } }