From 2308c1a7c7511ea2a804d62a9158b0e34d7cdd66 Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Tue, 10 Sep 2024 13:28:34 +0700 Subject: [PATCH] fix: error handling previously we do not properly catch errors in the swr methods. errorToast also threw an uncaught exception if it did not receive an error, which lead to the app hanging --- components/ToastConfig.tsx | 10 ++--- hooks/useBalance.ts | 11 +++++- hooks/useInfo.ts | 11 +++++- hooks/useTransactions.ts | 17 ++++++--- lib/errorToast.ts | 6 ++- lib/swr.ts | 3 +- pages/receive/Receive.tsx | 18 ++++----- pages/send/ConfirmPayment.tsx | 41 +++++++++++---------- pages/send/LNURLPay.tsx | 14 +++---- pages/send/Send.tsx | 9 ++--- pages/settings/wallets/WalletConnection.tsx | 4 +- 11 files changed, 81 insertions(+), 63 deletions(-) diff --git a/components/ToastConfig.tsx b/components/ToastConfig.tsx index d68ade5..cd4b8a7 100644 --- a/components/ToastConfig.tsx +++ b/components/ToastConfig.tsx @@ -13,20 +13,16 @@ export const toastConfig: ToastConfig = { {text1} - {text2 && - {text2} - } + {text2 && {text2}} ), error: ({ text1, text2, hide }) => ( - + {text1} - {text2 && - {text2} - } + {text2 && {text2}} ), connectionError: ({ text1, text2, hide }) => { diff --git a/hooks/useBalance.ts b/hooks/useBalance.ts index b643f4b..90fa221 100644 --- a/hooks/useBalance.ts +++ b/hooks/useBalance.ts @@ -1,13 +1,20 @@ import { useAppStore } from "lib/state/appStore"; import useSWR from "swr"; +import { errorToast } from "~/lib/errorToast"; type FetchArgs = Parameters; -const fetcher = (...args: FetchArgs) => { +const fetcher = async (...args: FetchArgs) => { const nwcClient = useAppStore.getState().nwcClient; if (!nwcClient) { throw new Error("No NWC client"); } - return nwcClient.getBalance(); + try { + const balance = await nwcClient.getBalance(); + return balance; + } catch (error) { + errorToast(error); + throw error; + } }; export function useBalance() { diff --git a/hooks/useInfo.ts b/hooks/useInfo.ts index db89bd3..f62618f 100644 --- a/hooks/useInfo.ts +++ b/hooks/useInfo.ts @@ -1,13 +1,20 @@ import { useAppStore } from "lib/state/appStore"; import useSWR from "swr"; +import { errorToast } from "~/lib/errorToast"; type FetchArgs = Parameters; -const fetcher = (...args: FetchArgs) => { +const fetcher = async (...args: FetchArgs) => { const nwcClient = useAppStore.getState().nwcClient; if (!nwcClient) { throw new Error("No NWC client"); } - return nwcClient.getInfo(); + try { + const info = await nwcClient.getInfo(); + return info; + } catch (error) { + errorToast(error); + throw error; + } }; export function useInfo() { diff --git a/hooks/useTransactions.ts b/hooks/useTransactions.ts index 574b15f..993d17e 100644 --- a/hooks/useTransactions.ts +++ b/hooks/useTransactions.ts @@ -1,10 +1,11 @@ import { useAppStore } from "lib/state/appStore"; import useSWR from "swr"; import { TRANSACTIONS_PAGE_SIZE } from "~/lib/constants"; +import { errorToast } from "~/lib/errorToast"; type FetchArgs = Parameters; -const fetcher = (...args: FetchArgs) => { +const fetcher = async (...args: FetchArgs) => { const nwcClient = useAppStore.getState().nwcClient; if (!nwcClient) { throw new Error("No NWC client"); @@ -13,10 +14,16 @@ const fetcher = (...args: FetchArgs) => { const transactionsUrl = new URL("http://" + (args[0] as string)); const page = +(transactionsUrl.searchParams.get("page") as string); - return nwcClient.listTransactions({ - limit: TRANSACTIONS_PAGE_SIZE, - offset: (page - 1) * TRANSACTIONS_PAGE_SIZE, - }); + try { + const transactions = await nwcClient.listTransactions({ + limit: TRANSACTIONS_PAGE_SIZE, + offset: (page - 1) * TRANSACTIONS_PAGE_SIZE, + }); + return transactions; + } catch (error) { + errorToast(error); + throw error; + } }; export function useTransactions(page = 1) { diff --git a/lib/errorToast.ts b/lib/errorToast.ts index f7820c8..d620adc 100644 --- a/lib/errorToast.ts +++ b/lib/errorToast.ts @@ -1,8 +1,10 @@ import Toast from "react-native-toast-message"; -export function errorToast(error: Error) { +export function errorToast(error: Error | unknown) { Toast.show({ type: "error", - text1: error.message, + text1: + (error as Error | undefined)?.message || + "An unknown error occured. Please check your internet connection or try restarting Alby Go", }); } diff --git a/lib/swr.ts b/lib/swr.ts index 20eb19d..ce69b84 100644 --- a/lib/swr.ts +++ b/lib/swr.ts @@ -1,5 +1,4 @@ -import { BareFetcher, SWRConfiguration } from "swr"; -import { useAppStore } from "./state/appStore"; +import { SWRConfiguration } from "swr"; // import AsyncStorage from "@react-native-async-storage/async-storage"; export const swrConfiguration: SWRConfiguration = { diff --git a/pages/receive/Receive.tsx b/pages/receive/Receive.tsx index 9454d14..66e4b37 100644 --- a/pages/receive/Receive.tsx +++ b/pages/receive/Receive.tsx @@ -58,7 +58,7 @@ export function Receive() { setEnterCustomAmount(false); } catch (error) { console.error(error); - errorToast(error as Error); + errorToast(error); } setLoading(false); })(); @@ -100,7 +100,7 @@ export function Receive() { polling && pollCount > 0 && receivedTransaction.payment_hash !== - prevTransaction?.payment_hash + prevTransaction?.payment_hash ) { if ( !invoiceRef.current || @@ -169,15 +169,13 @@ export function Receive() { }); } catch (error) { console.error("Error sharing:", error); - errorToast(error as Error); + errorToast(error); } } return ( <> - + {!enterCustomAmount && !invoice && !lightningAddress && ( <> @@ -242,7 +240,11 @@ export function Receive() { - @@ -311,5 +313,3 @@ export function Receive() { ); } - - diff --git a/pages/send/ConfirmPayment.tsx b/pages/send/ConfirmPayment.tsx index 7d8c1fc..2d72f9c 100644 --- a/pages/send/ConfirmPayment.tsx +++ b/pages/send/ConfirmPayment.tsx @@ -46,7 +46,7 @@ export function ConfirmPayment() { }); } catch (error) { console.error(error); - errorToast(error as Error); + errorToast(error); } setLoading(false); } @@ -56,9 +56,7 @@ export function ConfirmPayment() { }); return ( <> - + @@ -98,24 +96,29 @@ export function ConfirmPayment() { )} { /* only show "To" for lightning addresses */ originalText !== - invoice && - originalText - .toLowerCase() - .replace("lightning:", "") - .includes("@") && ( - - - To - - - {originalText.toLowerCase().replace("lightning:", "")} - - - ) + invoice && + originalText + .toLowerCase() + .replace("lightning:", "") + .includes("@") && ( + + + To + + + {originalText.toLowerCase().replace("lightning:", "")} + + + ) } -