From ac2505a5ef0aea70c6098a4bbfc995445116292a Mon Sep 17 00:00:00 2001 From: Leo Kewitz Date: Thu, 29 Feb 2024 17:46:19 +0100 Subject: [PATCH] refact: update getToken log to include connectedAccount.id --- server/lib/transferwise.ts | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/server/lib/transferwise.ts b/server/lib/transferwise.ts index 9757dd52551..3a219433e0b 100644 --- a/server/lib/transferwise.ts +++ b/server/lib/transferwise.ts @@ -97,21 +97,26 @@ export async function getToken(connectedAccount: ConnectedAccount, refresh = fal }; const refreshAndUpdateToken = async connectedAccount => { - const newToken = await getOrRefreshToken({ refreshToken: connectedAccount.refreshToken }); - if (!newToken) { - Activity.create({ - type: ActivityTypes.CONNECTED_ACCOUNT_ERROR, - data: { - connectedAccount: connectedAccount.activity, - error: 'There was an error refreshing the Wise token', - }, - CollectiveId: connectedAccount.CollectiveId, - }); - throw new Error('There was an error refreshing the Wise token'); + try { + const newToken = await getOrRefreshToken({ refreshToken: connectedAccount.refreshToken }); + if (!newToken) { + Activity.create({ + type: ActivityTypes.CONNECTED_ACCOUNT_ERROR, + data: { + connectedAccount: connectedAccount.activity, + error: 'There was an error refreshing the Wise token', + }, + CollectiveId: connectedAccount.CollectiveId, + }); + throw new Error('There was an error refreshing the Wise token'); + } + const { access_token: token, refresh_token: refreshToken, ...data } = newToken; + await connectedAccount.update({ token, refreshToken, data: { ...connectedAccount.data, ...data } }); + return token; + } catch (e) { + logger.error(`Error refreshing Wise token for connected account #${connectedAccount.id}`, e); + throw e; } - const { access_token: token, refresh_token: refreshToken, ...data } = newToken; - await connectedAccount.update({ token, refreshToken, data: { ...connectedAccount.data, ...data } }); - return token; }; if (!checkTokenIsExpired(connectedAccount)) { @@ -773,9 +778,8 @@ export const getOrRefreshToken = async ({ return token; } catch (e) { debug(JSON.stringify(e)); - const error = parseError(e, "There was an error while refreshing host's Wise token"); - logger.error(error.toString()); reportErrorToSentry(e, { feature: FEATURE.TRANSFERWISE }); + const error = parseError(e, "There was an error while refreshing host's Wise token"); throw error; } };