From 534970d475c73eaac87aebfe4cfb3739d0930bf7 Mon Sep 17 00:00:00 2001 From: Bill He Date: Wed, 8 Nov 2023 13:28:54 -0800 Subject: [PATCH] Handle squid route errors --- src/lib/abacus/rest.ts | 9 ++------- .../forms/AccountManagementForms/DepositForm.tsx | 12 ++++++++++++ .../forms/AccountManagementForms/WithdrawForm.tsx | 12 ++++++++++++ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/lib/abacus/rest.ts b/src/lib/abacus/rest.ts index 52e5ca4..12245f5 100644 --- a/src/lib/abacus/rest.ts +++ b/src/lib/abacus/rest.ts @@ -56,13 +56,8 @@ class AbacusRest implements AbacusRestProtocol { .then(async (response) => { const data = await response.text(); - if (response.ok) { - callback(data, response.status); - } else { - // response not OK, call callback with null data and the status, this includes 400/500 status codes - callback(null, response.status); - } - + callback(data, response.status); + try { lastSuccessfulRestRequestByOrigin[new URL(url).origin] = Date.now(); } catch {} diff --git a/src/views/forms/AccountManagementForms/DepositForm.tsx b/src/views/forms/AccountManagementForms/DepositForm.tsx index d7ce376..a2d9850 100644 --- a/src/views/forms/AccountManagementForms/DepositForm.tsx +++ b/src/views/forms/AccountManagementForms/DepositForm.tsx @@ -65,6 +65,7 @@ export const DepositForm = ({ onDeposit, onError }: DepositFormProps) => { chain: chainIdStr, resources, summary, + errors: routeErrors, } = useSelector(getTransferInputs, shallowEqual) || {}; const chainId = chainIdStr ? parseInt(chainIdStr) : undefined; @@ -287,6 +288,17 @@ export const DepositForm = ({ onDeposit, onError }: DepositFormProps) => { return parseWalletError({ error, stringGetter }).message; } + if (routeErrors) { + const parsedErrors = JSON.parse(routeErrors); + if (parsedErrors?.[0]?.message) { + return stringGetter({ + key: STRING_KEYS.SOMETHING_WENT_WRONG_WITH_MESSAGE, + params: { ERROR_MESSAGE: parsedErrors?.[0]?.message }, + }); + } + } + + if (fromAmount) { if (!chainId) { return stringGetter({ key: STRING_KEYS.MUST_SPECIFY_CHAIN }); diff --git a/src/views/forms/AccountManagementForms/WithdrawForm.tsx b/src/views/forms/AccountManagementForms/WithdrawForm.tsx index f6fb8ff..35f9cae 100644 --- a/src/views/forms/AccountManagementForms/WithdrawForm.tsx +++ b/src/views/forms/AccountManagementForms/WithdrawForm.tsx @@ -69,6 +69,7 @@ export const WithdrawForm = () => { chain: chainIdStr, address: toAddress, resources, + errors: routeErrors, } = useSelector(getTransferInputs, shallowEqual) || {}; const isValidAddress = toAddress && isAddress(toAddress); @@ -281,6 +282,16 @@ export const WithdrawForm = () => { }); } + if (routeErrors) { + const parsedErrors = JSON.parse(routeErrors); + if (parsedErrors?.[0]?.message) { + return stringGetter({ + key: STRING_KEYS.SOMETHING_WENT_WRONG_WITH_MESSAGE, + params: { ERROR_MESSAGE: parsedErrors?.[0]?.message }, + }); + } + } + if (!toAddress) return stringGetter({ key: STRING_KEYS.WITHDRAW_MUST_SPECIFY_ADDRESS }); if (sanctionedAddresses.has(toAddress)) @@ -303,6 +314,7 @@ export const WithdrawForm = () => { return undefined; }, [ error, + routeErrors, freeCollateralBN, chainIdStr, debouncedAmountBN,