From a6a33a09e0c11a4fd669c3afc7716015a32525af Mon Sep 17 00:00:00 2001 From: kyranjamie Date: Mon, 5 Feb 2024 13:55:29 +0100 Subject: [PATCH] refactor: error helper --- src/app/common/hooks/use-submit-stx-transaction.ts | 3 ++- src/app/common/utils/safe-await.ts | 3 ++- .../increase-fee-drawer/hooks/use-btc-increase-fee.ts | 3 ++- .../ledger-bitcoin-sign-tx-container.tsx | 3 ++- .../flows/jwt-signing/ledger-sign-jwt-container.tsx | 3 ++- .../ledger-stacks-sign-msg-container.tsx | 3 ++- .../stacks-tx-signing/ledger-sign-stacks-tx-container.tsx | 5 +++-- .../generic-flows/request-keys/use-request-ledger-keys.ts | 3 ++- src/app/features/psbt-signer/psbt-signer.tsx | 4 ++-- .../hooks/use-stacks-broadcast-transaction.tsx | 4 ++-- src/app/pages/psbt-request/use-psbt-request.tsx | 3 ++- src/app/pages/rpc-sign-psbt/use-rpc-sign-psbt.tsx | 8 ++++---- .../hooks/use-send-inscription-form.tsx | 3 ++- src/app/pages/swap/hooks/use-stacks-broadcast-swap.tsx | 4 ++-- src/shared/utils.ts | 4 ++++ 15 files changed, 35 insertions(+), 21 deletions(-) diff --git a/src/app/common/hooks/use-submit-stx-transaction.ts b/src/app/common/hooks/use-submit-stx-transaction.ts index 56de95a7992..7f64bd24286 100644 --- a/src/app/common/hooks/use-submit-stx-transaction.ts +++ b/src/app/common/hooks/use-submit-stx-transaction.ts @@ -5,6 +5,7 @@ import { bytesToHex } from '@stacks/common'; import { StacksTransaction, broadcastTransaction } from '@stacks/transactions'; import { logger } from '@shared/logger'; +import { isError } from '@shared/utils'; import { getErrorMessage } from '@app/common/get-error-message'; import { useRefreshAllAccountData } from '@app/common/hooks/account/use-refresh-all-account-data'; @@ -59,7 +60,7 @@ export function useSubmitTransactionCallback({ loadingKey }: UseSubmitTransactio } } catch (error) { logger.error('Transaction callback', { error }); - onError(error instanceof Error ? error : { name: '', message: '' }); + onError(isError(error) ? error : { name: '', message: '' }); setIsIdle(); } }, diff --git a/src/app/common/utils/safe-await.ts b/src/app/common/utils/safe-await.ts index 304aff22a0c..e0bcec9c54c 100644 --- a/src/app/common/utils/safe-await.ts +++ b/src/app/common/utils/safe-await.ts @@ -1,4 +1,5 @@ // TypeScript port of https://github.com/DavidWells/safe-await/ +import { isError } from '@shared/utils'; // Native Error types https://mzl.la/2Veh3TR const nativeExceptions = [ @@ -19,7 +20,7 @@ function throwNative(error: Error) { export async function safeAwait(promise: Promise, finallyFn?: () => void) { return promise .then(data => { - if (data instanceof Error) { + if (isError(data)) { throwNative(data); return [data] as readonly [Error]; } diff --git a/src/app/features/increase-fee-drawer/hooks/use-btc-increase-fee.ts b/src/app/features/increase-fee-drawer/hooks/use-btc-increase-fee.ts index bbd86d8eef3..212ec4d01f1 100644 --- a/src/app/features/increase-fee-drawer/hooks/use-btc-increase-fee.ts +++ b/src/app/features/increase-fee-drawer/hooks/use-btc-increase-fee.ts @@ -8,6 +8,7 @@ import * as yup from 'yup'; import { createMoney } from '@shared/models/money.model'; import { BitcoinTx } from '@shared/models/transactions/bitcoin-transaction.model'; import { RouteUrls } from '@shared/route-urls'; +import { isError } from '@shared/utils'; import { useAnalytics } from '@app/common/hooks/analytics/use-analytics'; import { useBtcAssetBalance } from '@app/common/hooks/balance/btc/use-btc-balance'; @@ -122,7 +123,7 @@ export function useBtcIncreaseFee(btcTx: BitcoinTx) { } function onError(error: unknown) { - const message = error instanceof Error ? error.message : 'Unknown error'; + const message = isError(error) ? error.message : 'Unknown error'; toast.error(message); navigate(RouteUrls.Home); } diff --git a/src/app/features/ledger/flows/bitcoin-tx-signing/ledger-bitcoin-sign-tx-container.tsx b/src/app/features/ledger/flows/bitcoin-tx-signing/ledger-bitcoin-sign-tx-container.tsx index d6203b39eff..25a6c802f1b 100644 --- a/src/app/features/ledger/flows/bitcoin-tx-signing/ledger-bitcoin-sign-tx-container.tsx +++ b/src/app/features/ledger/flows/bitcoin-tx-signing/ledger-bitcoin-sign-tx-container.tsx @@ -9,6 +9,7 @@ import get from 'lodash.get'; import { BitcoinInputSigningConfig } from '@shared/crypto/bitcoin/signer-config'; import { logger } from '@shared/logger'; import { RouteUrls } from '@shared/route-urls'; +import { isError } from '@shared/utils'; import { useLocationState, useLocationStateWithCache } from '@app/common/hooks/use-location-state'; import { useScrollLock } from '@app/common/hooks/use-scroll-lock'; @@ -115,7 +116,7 @@ function LedgerSignBitcoinTxContainer() { void bitcoinApp.transport.close(); } } catch (e) { - if (e instanceof Error && checkLockedDeviceError(e)) { + if (isError(e) && checkLockedDeviceError(e)) { setLatestDeviceResponse({ deviceLocked: true } as any); return; } diff --git a/src/app/features/ledger/flows/jwt-signing/ledger-sign-jwt-container.tsx b/src/app/features/ledger/flows/jwt-signing/ledger-sign-jwt-container.tsx index d7bcccf80eb..5d7a7a9a3ff 100644 --- a/src/app/features/ledger/flows/jwt-signing/ledger-sign-jwt-container.tsx +++ b/src/app/features/ledger/flows/jwt-signing/ledger-sign-jwt-container.tsx @@ -7,6 +7,7 @@ import get from 'lodash.get'; import { finalizeAuthResponse } from '@shared/actions/finalize-auth-response'; import { logger } from '@shared/logger'; +import { isError } from '@shared/utils'; import { useGetLegacyAuthBitcoinAddresses } from '@app/common/authentication/use-legacy-auth-bitcoin-addresses'; import { useOnboardingState } from '@app/common/hooks/auth/use-onboarding-state'; @@ -92,7 +93,7 @@ export function LedgerSignJwtContainer() { const stacks = await prepareLedgerDeviceStacksAppConnection({ setLoadingState: setAwaitingDeviceConnection, onError(e) { - if (e instanceof Error && checkLockedDeviceError(e)) { + if (isError(e) && checkLockedDeviceError(e)) { setLatestDeviceResponse({ deviceLocked: true } as any); return; } diff --git a/src/app/features/ledger/flows/stacks-message-signing/ledger-stacks-sign-msg-container.tsx b/src/app/features/ledger/flows/stacks-message-signing/ledger-stacks-sign-msg-container.tsx index 855814cc58c..1b969cc860e 100644 --- a/src/app/features/ledger/flows/stacks-message-signing/ledger-stacks-sign-msg-container.tsx +++ b/src/app/features/ledger/flows/stacks-message-signing/ledger-stacks-sign-msg-container.tsx @@ -9,6 +9,7 @@ import get from 'lodash.get'; import { finalizeMessageSignature } from '@shared/actions/finalize-message-signature'; import { logger } from '@shared/logger'; import { UnsignedMessage, whenSignableMessageOfType } from '@shared/signature/signature-types'; +import { isError } from '@shared/utils'; import { useScrollLock } from '@app/common/hooks/use-scroll-lock'; import { delay } from '@app/common/utils'; @@ -67,7 +68,7 @@ function LedgerSignStacksMsg({ account, unsignedMessage }: LedgerSignMsgProps) { const stacksApp = await prepareLedgerDeviceStacksAppConnection({ setLoadingState: setAwaitingDeviceConnection, onError(e) { - if (e instanceof Error && checkLockedDeviceError(e)) { + if (isError(e) && checkLockedDeviceError(e)) { setLatestDeviceResponse({ deviceLocked: true } as any); return; } diff --git a/src/app/features/ledger/flows/stacks-tx-signing/ledger-sign-stacks-tx-container.tsx b/src/app/features/ledger/flows/stacks-tx-signing/ledger-sign-stacks-tx-container.tsx index a4809554c99..9eae8e1df09 100644 --- a/src/app/features/ledger/flows/stacks-tx-signing/ledger-sign-stacks-tx-container.tsx +++ b/src/app/features/ledger/flows/stacks-tx-signing/ledger-sign-stacks-tx-container.tsx @@ -7,6 +7,7 @@ import get from 'lodash.get'; import { logger } from '@shared/logger'; import { RouteUrls } from '@shared/route-urls'; +import { isError } from '@shared/utils'; import { useScrollLock } from '@app/common/hooks/use-scroll-lock'; import { appEvents } from '@app/common/publish-subscribe'; @@ -71,7 +72,7 @@ function LedgerSignStacksTxContainer() { const stacksApp = await prepareLedgerDeviceStacksAppConnection({ setLoadingState: setAwaitingDeviceConnection, onError(e) { - if (e instanceof Error && checkLockedDeviceError(e)) { + if (isError(e) && checkLockedDeviceError(e)) { setLatestDeviceResponse({ deviceLocked: true } as any); return; } @@ -148,7 +149,7 @@ function LedgerSignStacksTxContainer() { signedTx, }); } catch (e) { - ledgerNavigate.toBroadcastErrorStep(e instanceof Error ? e.message : 'Unknown error'); + ledgerNavigate.toBroadcastErrorStep(isError(e) ? e.message : 'Unknown error'); return; } } catch (e) { diff --git a/src/app/features/ledger/generic-flows/request-keys/use-request-ledger-keys.ts b/src/app/features/ledger/generic-flows/request-keys/use-request-ledger-keys.ts index 36a007db0bd..c73de72b4ec 100644 --- a/src/app/features/ledger/generic-flows/request-keys/use-request-ledger-keys.ts +++ b/src/app/features/ledger/generic-flows/request-keys/use-request-ledger-keys.ts @@ -4,6 +4,7 @@ import StacksApp from '@zondax/ledger-stacks'; import AppClient from 'ledger-bitcoin'; import { SupportedBlockchains } from '@shared/constants'; +import { isError } from '@shared/utils'; import { delay } from '@app/common/utils'; @@ -73,7 +74,7 @@ export function useRequestLedgerKeys({ onSuccess?.(); } catch (e) { setAwaitingDeviceConnection(false); - if (e instanceof Error && checkLockedDeviceError(e)) { + if (isError(e) && checkLockedDeviceError(e)) { setLatestDeviceResponse({ deviceLocked: true } as any); return; } diff --git a/src/app/features/psbt-signer/psbt-signer.tsx b/src/app/features/psbt-signer/psbt-signer.tsx index 0b59788e404..85dc31a1fdb 100644 --- a/src/app/features/psbt-signer/psbt-signer.tsx +++ b/src/app/features/psbt-signer/psbt-signer.tsx @@ -3,7 +3,7 @@ import { useNavigate } from 'react-router-dom'; import { getPsbtTxInputs, getPsbtTxOutputs } from '@shared/crypto/bitcoin/bitcoin.utils'; import { RouteUrls } from '@shared/route-urls'; -import { closeWindow } from '@shared/utils'; +import { closeWindow, isError } from '@shared/utils'; import { useRouteHeader } from '@app/common/hooks/use-route-header'; import { SignPsbtArgs } from '@app/common/psbt/requests'; @@ -51,7 +51,7 @@ export function PsbtSigner(props: PsbtSignerProps) { return getRawPsbt(psbtHex); } catch (e) { navigate(RouteUrls.RequestError, { - state: { message: e instanceof Error ? e.message : '', title: 'Failed request' }, + state: { message: isError(e) ? e.message : '', title: 'Failed request' }, }); return; } diff --git a/src/app/features/stacks-transaction-request/hooks/use-stacks-broadcast-transaction.tsx b/src/app/features/stacks-transaction-request/hooks/use-stacks-broadcast-transaction.tsx index b25518bf685..3aaf8850c3f 100644 --- a/src/app/features/stacks-transaction-request/hooks/use-stacks-broadcast-transaction.tsx +++ b/src/app/features/stacks-transaction-request/hooks/use-stacks-broadcast-transaction.tsx @@ -7,7 +7,7 @@ import { StacksTransaction } from '@stacks/transactions'; import { logger } from '@shared/logger'; import { CryptoCurrencies } from '@shared/models/currencies.model'; import { RouteUrls } from '@shared/route-urls'; -import { isString } from '@shared/utils'; +import { isError, isString } from '@shared/utils'; import { LoadingKeys } from '@app/common/hooks/use-loading'; import { useSubmitTransactionCallback } from '@app/common/hooks/use-submit-stx-transaction'; @@ -56,7 +56,7 @@ export function useStacksBroadcastTransaction(token: CryptoCurrencies, decimals? })(signedTx); } catch (e) { navigate(RouteUrls.TransactionBroadcastError, { - state: { message: e instanceof Error ? e.message : 'Unknown error' }, + state: { message: isError(e) ? e.message : 'Unknown error' }, }); } finally { setIsBroadcasting(false); diff --git a/src/app/pages/psbt-request/use-psbt-request.tsx b/src/app/pages/psbt-request/use-psbt-request.tsx index 144e46adf37..8eb6e280f1a 100644 --- a/src/app/pages/psbt-request/use-psbt-request.tsx +++ b/src/app/pages/psbt-request/use-psbt-request.tsx @@ -5,6 +5,7 @@ import { bytesToHex, hexToBytes } from '@noble/hashes/utils'; import { finalizePsbt } from '@shared/actions/finalize-psbt'; import { RouteUrls } from '@shared/route-urls'; +import { isError } from '@shared/utils'; import { useAnalytics } from '@app/common/hooks/analytics/use-analytics'; import { usePsbtRequestSearchParams } from '@app/common/psbt/use-psbt-request-params'; @@ -57,7 +58,7 @@ export function usePsbtRequest() { }); } catch (e) { return navigate(RouteUrls.RequestError, { - state: { message: e instanceof Error ? e.message : '', title: 'Failed to sign' }, + state: { message: isError(e) ? e.message : '', title: 'Failed to sign' }, }); } }, diff --git a/src/app/pages/rpc-sign-psbt/use-rpc-sign-psbt.tsx b/src/app/pages/rpc-sign-psbt/use-rpc-sign-psbt.tsx index 4c43efa4b8e..222f4033027 100644 --- a/src/app/pages/rpc-sign-psbt/use-rpc-sign-psbt.tsx +++ b/src/app/pages/rpc-sign-psbt/use-rpc-sign-psbt.tsx @@ -7,7 +7,7 @@ import { bytesToHex } from '@stacks/common'; import { Money } from '@shared/models/money.model'; import { RouteUrls } from '@shared/route-urls'; import { makeRpcErrorResponse, makeRpcSuccessResponse } from '@shared/rpc/rpc-methods'; -import { closeWindow } from '@shared/utils'; +import { closeWindow, isError } from '@shared/utils'; import { useAnalytics } from '@app/common/hooks/analytics/use-analytics'; import { sumMoney } from '@app/common/money/calculate-money'; @@ -78,7 +78,7 @@ export function useRpcSignPsbt() { }, onError(e) { navigate(RouteUrls.RequestError, { - state: { message: e instanceof Error ? e.message : '', title: 'Failed to broadcast' }, + state: { message: isError(e) ? e.message : '', title: 'Failed to broadcast' }, }); }, }); @@ -113,7 +113,7 @@ export function useRpcSignPsbt() { } catch (e) { return navigate(RouteUrls.RequestError, { state: { - message: e instanceof Error ? e.message : '', + message: isError(e) ? e.message : '', title: 'Failed to finalize tx', }, }); @@ -130,7 +130,7 @@ export function useRpcSignPsbt() { closeWindow(); } catch (e) { return navigate(RouteUrls.RequestError, { - state: { message: e instanceof Error ? e.message : '', title: 'Failed to sign' }, + state: { message: isError(e) ? e.message : '', title: 'Failed to sign' }, }); } }, diff --git a/src/app/pages/send/ordinal-inscription/hooks/use-send-inscription-form.tsx b/src/app/pages/send/ordinal-inscription/hooks/use-send-inscription-form.tsx index 5aa4859b6b8..f60660bf7d2 100644 --- a/src/app/pages/send/ordinal-inscription/hooks/use-send-inscription-form.tsx +++ b/src/app/pages/send/ordinal-inscription/hooks/use-send-inscription-form.tsx @@ -6,6 +6,7 @@ import * as yup from 'yup'; import { logger } from '@shared/logger'; import { OrdinalSendFormValues } from '@shared/models/form.model'; import { RouteUrls } from '@shared/route-urls'; +import { isError } from '@shared/utils'; import { FormErrorMessages } from '@app/common/error-messages'; import { useAnalytics } from '@app/common/hooks/analytics/use-analytics'; @@ -66,7 +67,7 @@ export function useSendInscriptionForm() { void analytics.track('ordinals_dot_com_unavailable', { error }); let message = 'Unable to establish if utxo has multiple inscriptions'; - if (error instanceof Error) { + if (isError(error)) { message = error.message; } setShowError(message); diff --git a/src/app/pages/swap/hooks/use-stacks-broadcast-swap.tsx b/src/app/pages/swap/hooks/use-stacks-broadcast-swap.tsx index 551eb4598b3..4046cd2ec20 100644 --- a/src/app/pages/swap/hooks/use-stacks-broadcast-swap.tsx +++ b/src/app/pages/swap/hooks/use-stacks-broadcast-swap.tsx @@ -6,7 +6,7 @@ import { StacksTransaction } from '@stacks/transactions'; import { logger } from '@shared/logger'; import { RouteUrls } from '@shared/route-urls'; -import { isString } from '@shared/utils'; +import { isError, isString } from '@shared/utils'; import { LoadingKeys, useLoading } from '@app/common/hooks/use-loading'; import { useSubmitTransactionCallback } from '@app/common/hooks/use-submit-stx-transaction'; @@ -42,7 +42,7 @@ export function useStacksBroadcastSwap() { } catch (e) { setIsIdle(); navigate(RouteUrls.TransactionBroadcastError, { - state: { message: e instanceof Error ? e.message : 'Unknown error' }, + state: { message: isError(e) ? e.message : 'Unknown error' }, }); } finally { setIsIdle(); diff --git a/src/shared/utils.ts b/src/shared/utils.ts index 34e35814db7..9605679b59e 100644 --- a/src/shared/utils.ts +++ b/src/shared/utils.ts @@ -29,6 +29,10 @@ export function isObject(value: unknown): value is object { return typeof value === 'object'; } +export function isError(value: unknown): value is Error { + return value instanceof Error; +} + export function isEmpty(value: object) { return Object.keys(value).length === 0; }