From 12f4295932efdb9f38b4e381a3e8842535cd8171 Mon Sep 17 00:00:00 2001 From: Robert Dyer Date: Mon, 5 Aug 2024 04:13:43 -0500 Subject: [PATCH] Stop server crashing when SimpleFIN is down. (#410) --- src/app-simplefin/app-simplefin.js | 40 ++++++++++++++++++++++++------ upcoming-release-notes/410.md | 6 +++++ 2 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 upcoming-release-notes/410.md diff --git a/src/app-simplefin/app-simplefin.js b/src/app-simplefin/app-simplefin.js index 5d8693ebf..31d10439c 100644 --- a/src/app-simplefin/app-simplefin.js +++ b/src/app-simplefin/app-simplefin.js @@ -50,14 +50,19 @@ app.post( const startDate = new Date(now.getFullYear(), now.getMonth(), 1); const endDate = new Date(now.getFullYear(), now.getMonth() + 1, 1); - const accounts = await getAccounts(accessKey, startDate, endDate); + try { + const accounts = await getAccounts(accessKey, startDate, endDate); - res.send({ - status: 'ok', - data: { - accounts: accounts.accounts, - }, - }); + res.send({ + status: 'ok', + data: { + accounts: accounts.accounts, + }, + }); + } catch (e) { + serverDown(e, res); + return; + } }), ); @@ -73,9 +78,15 @@ app.post( return; } + let results; try { - const results = await getTransactions(accessKey, new Date(startDate)); + results = await getTransactions(accessKey, new Date(startDate)); + } catch (e) { + serverDown(e, res); + return; + } + try { const account = results.accounts.find((a) => a.id === accountId); const needsAttention = results.errors.find( @@ -194,6 +205,19 @@ function invalidToken(res) { }); } +function serverDown(e, res) { + console.log(e); + res.send({ + status: 'ok', + data: { + error_type: 'SERVER_DOWN', + error_code: 'SERVER_DOWN', + status: 'rejected', + reason: 'There was an error communciating with SimpleFIN.', + }, + }); +} + function parseAccessKey(accessKey) { let scheme = null; let rest = null; diff --git a/upcoming-release-notes/410.md b/upcoming-release-notes/410.md new file mode 100644 index 000000000..4896a59dc --- /dev/null +++ b/upcoming-release-notes/410.md @@ -0,0 +1,6 @@ +--- +category: Bugfix +authors: [psybers] +--- + +Stop server crashing when SimpleFIN is down.