Skip to content

Commit

Permalink
Merge pull request #101 from getAlby/fix/error-handling
Browse files Browse the repository at this point in the history
fix: error handling
  • Loading branch information
rolznz authored Sep 10, 2024
2 parents efd921b + 2308c1a commit b8a295b
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 63 deletions.
10 changes: 3 additions & 7 deletions components/ToastConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,16 @@ export const toastConfig: ToastConfig = {
<CheckCircle className="text-background" width={16} height={16} />
<Text className="text-background font-semibold2">{text1}</Text>
</View>
{text2 &&
<Text className="text-background text-center">{text2}</Text>
}
{text2 && <Text className="text-background text-center">{text2}</Text>}
</View>
),
error: ({ text1, text2, hide }) => (
<View className="bg-foreground rounded-full px-6 py-3 mx-6">
<View className="bg-destructive rounded-full px-6 py-3 mx-6">
<View className="flex flex-row gap-2 justify-center items-center">
<XCircle className="text-background" width={16} height={16} />
<Text className="text-background font-semibold2">{text1}</Text>
</View>
{text2 &&
<Text className="text-background text-center">{text2}</Text>
}
{text2 && <Text className="text-background text-center">{text2}</Text>}
</View>
),
connectionError: ({ text1, text2, hide }) => {
Expand Down
11 changes: 9 additions & 2 deletions hooks/useBalance.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { useAppStore } from "lib/state/appStore";
import useSWR from "swr";
import { errorToast } from "~/lib/errorToast";

type FetchArgs = Parameters<typeof fetch>;
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() {
Expand Down
11 changes: 9 additions & 2 deletions hooks/useInfo.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { useAppStore } from "lib/state/appStore";
import useSWR from "swr";
import { errorToast } from "~/lib/errorToast";

type FetchArgs = Parameters<typeof fetch>;
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() {
Expand Down
17 changes: 12 additions & 5 deletions hooks/useTransactions.ts
Original file line number Diff line number Diff line change
@@ -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<typeof fetch>;

const fetcher = (...args: FetchArgs) => {
const fetcher = async (...args: FetchArgs) => {
const nwcClient = useAppStore.getState().nwcClient;
if (!nwcClient) {
throw new Error("No NWC client");
Expand All @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions lib/errorToast.ts
Original file line number Diff line number Diff line change
@@ -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",
});
}
3 changes: 1 addition & 2 deletions lib/swr.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
18 changes: 9 additions & 9 deletions pages/receive/Receive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function Receive() {
setEnterCustomAmount(false);
} catch (error) {
console.error(error);
errorToast(error as Error);
errorToast(error);
}
setLoading(false);
})();
Expand Down Expand Up @@ -100,7 +100,7 @@ export function Receive() {
polling &&
pollCount > 0 &&
receivedTransaction.payment_hash !==
prevTransaction?.payment_hash
prevTransaction?.payment_hash
) {
if (
!invoiceRef.current ||
Expand Down Expand Up @@ -169,15 +169,13 @@ export function Receive() {
});
} catch (error) {
console.error("Error sharing:", error);
errorToast(error as Error);
errorToast(error);
}
}

return (
<>
<Screen
title="Receive"
/>
<Screen title="Receive" />
{!enterCustomAmount && !invoice && !lightningAddress && (
<>
<View className="flex-1 h-full flex flex-col items-center justify-center gap-5">
Expand Down Expand Up @@ -242,7 +240,11 @@ export function Receive() {
</View>

<View className="flex flex-row gap-3 p-6">
<Button onPress={share} variant="secondary" className="flex-1 flex flex-col gap-2">
<Button
onPress={share}
variant="secondary"
className="flex-1 flex flex-col gap-2"
>
<Share2 className="text-muted-foreground" />
<Text>Share</Text>
</Button>
Expand Down Expand Up @@ -311,5 +313,3 @@ export function Receive() {
</>
);
}


41 changes: 22 additions & 19 deletions pages/send/ConfirmPayment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function ConfirmPayment() {
});
} catch (error) {
console.error(error);
errorToast(error as Error);
errorToast(error);
}
setLoading(false);
}
Expand All @@ -56,9 +56,7 @@ export function ConfirmPayment() {
});
return (
<>
<Screen
title="Confirm Payment"
/>
<Screen title="Confirm Payment" />
<View className="flex-1 justify-center items-center gap-8 p-6">
<View className="flex flex-col gap-2">
<View className="flex flex-row items-center justify-center gap-2">
Expand Down Expand Up @@ -98,24 +96,29 @@ export function ConfirmPayment() {
)}
{
/* only show "To" for lightning addresses */ originalText !==
invoice &&
originalText
.toLowerCase()
.replace("lightning:", "")
.includes("@") && (
<View className="flex flex-col gap-2">
<Text className="text-muted-foreground text-center font-semibold2">
To
</Text>
<Text className="text-center text-foreground text-2xl font-medium2">
{originalText.toLowerCase().replace("lightning:", "")}
</Text>
</View>
)
invoice &&
originalText
.toLowerCase()
.replace("lightning:", "")
.includes("@") && (
<View className="flex flex-col gap-2">
<Text className="text-muted-foreground text-center font-semibold2">
To
</Text>
<Text className="text-center text-foreground text-2xl font-medium2">
{originalText.toLowerCase().replace("lightning:", "")}
</Text>
</View>
)
}
</View>
<View className="p-6">
<Button size="lg" onPress={pay} className="flex flex-row gap-2" disabled={isLoading}>
<Button
size="lg"
onPress={pay}
className="flex flex-row gap-2"
disabled={isLoading}
>
{isLoading ? (
<Loading className="text-primary-foreground" />
) : (
Expand Down
14 changes: 7 additions & 7 deletions pages/send/LNURLPay.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import Screen from "~/components/Screen";
import { router, useLocalSearchParams } from "expo-router";
import React from "react";
Expand Down Expand Up @@ -39,24 +38,26 @@ export function LNURLPay() {
});
} catch (error) {
console.error(error);
errorToast(error as Error);
errorToast(error);
}
setLoading(false);
}

return (
<>
<Screen
title="Send"
/>
<Screen title="Send" />
<TouchableWithoutFeedback
onPress={() => {
Keyboard.dismiss();
}}
>
<View className="flex-1 flex flex-col">
<View className="flex-1 justify-center items-center p-6 gap-6">
<DualCurrencyInput amount={amount} setAmount={setAmount} autoFocus />
<DualCurrencyInput
amount={amount}
setAmount={setAmount}
autoFocus
/>
<View className="w-full">
<Text className="text-muted-foreground text-center font-semibold2">
Comment
Expand Down Expand Up @@ -91,7 +92,6 @@ export function LNURLPay() {
</View>
</View>
</TouchableWithoutFeedback>

</>
);
}
9 changes: 3 additions & 6 deletions pages/send/Send.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import Screen from "~/components/Screen";
import React, { useEffect } from "react";
import { Keyboard, TouchableWithoutFeedback, View } from "react-native";
Expand Down Expand Up @@ -103,16 +102,14 @@ export function Send() {
}
} catch (error) {
console.error("failed to load payment", originalText, error);
errorToast(error as Error);
errorToast(error);
setLoading(false);
}
}

return (
<>
<Screen
title="Send"
/>
<Screen title="Send" />
{isLoading && (
<View className="flex-1 flex flex-col items-center justify-center">
<Loading className="text-primary-foreground" />
Expand Down Expand Up @@ -173,7 +170,7 @@ export function Send() {
inputMode="email"
autoFocus
returnKeyType="done"
// aria-errormessage="inputError"
// aria-errormessage="inputError"
/>
</View>
<Button onPress={submitKeyboardText} size="lg">
Expand Down
4 changes: 2 additions & 2 deletions pages/settings/wallets/WalletConnection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function WalletConnection() {
nostrWalletConnectUrl = await Clipboard.getStringAsync();
} catch (error) {
console.error("Failed to read clipboard", error);
errorToast(error as Error);
errorToast(error);
return;
}
connect(nostrWalletConnectUrl);
Expand Down Expand Up @@ -81,7 +81,7 @@ export function WalletConnection() {
});
} catch (error) {
console.error(error);
errorToast(error as Error);
errorToast(error);
}
setConnecting(false);
}
Expand Down

0 comments on commit b8a295b

Please sign in to comment.