From 520f3d7e7ed7999250ee37163b5dd81175ba4caa Mon Sep 17 00:00:00 2001 From: 0xKheops <26880866+0xKheops@users.noreply.github.com> Date: Mon, 6 Jan 2025 11:57:58 +0900 Subject: [PATCH] refactor: single method for wrapping ledger errors --- .../Shared/ConnectLedgerBase.tsx | 4 ++-- .../Account/LedgerEthereumAccountPicker.tsx | 4 ++-- .../LedgerSubstrateGenericAccountPicker.tsx | 6 +++--- .../LedgerSubstrateLegacyAccountPicker.tsx | 4 ++-- .../src/ui/domains/Sign/SignLedgerEthereum.tsx | 6 +++--- .../Sign/SignLedgerSubstrateGeneric.tsx | 4 ++-- .../domains/Sign/SignLedgerSubstrateLegacy.tsx | 6 +++--- apps/extension/src/ui/hooks/ledger/errors.ts | 18 ++++++++---------- .../hooks/ledger/useLedgerSubstrateGeneric.ts | 12 +++++------- 9 files changed, 30 insertions(+), 34 deletions(-) diff --git a/apps/extension/src/ui/domains/Account/AccountAdd/AccountAddLedger/Shared/ConnectLedgerBase.tsx b/apps/extension/src/ui/domains/Account/AccountAdd/AccountAddLedger/Shared/ConnectLedgerBase.tsx index b8653064f..bfb1d58d8 100644 --- a/apps/extension/src/ui/domains/Account/AccountAdd/AccountAddLedger/Shared/ConnectLedgerBase.tsx +++ b/apps/extension/src/ui/domains/Account/AccountAdd/AccountAddLedger/Shared/ConnectLedgerBase.tsx @@ -7,7 +7,7 @@ import { LedgerConnectionStatus, LedgerConnectionStatusProps, } from "@ui/domains/Account/LedgerConnectionStatus" -import { getCustomTalismanLedgerError } from "@ui/hooks/ledger/errors" +import { getTalismanLedgerError } from "@ui/hooks/ledger/errors" type ConnectLedgerBaseProps = { appName: string @@ -51,7 +51,7 @@ export const ConnectLedgerBase: FC = ({ }) onReadyChanged?.(true) } catch (err) { - const error = getCustomTalismanLedgerError(err) + const error = getTalismanLedgerError(err) log.error("ConnectLedgerSubstrateGeneric", { error }) setConnectionStatus({ status: "error", diff --git a/apps/extension/src/ui/domains/Account/LedgerEthereumAccountPicker.tsx b/apps/extension/src/ui/domains/Account/LedgerEthereumAccountPicker.tsx index 74c179bd9..efd1d4686 100644 --- a/apps/extension/src/ui/domains/Account/LedgerEthereumAccountPicker.tsx +++ b/apps/extension/src/ui/domains/Account/LedgerEthereumAccountPicker.tsx @@ -5,7 +5,7 @@ import { getEthLedgerDerivationPath, LedgerEthDerivationPathType } from "@extens import { log } from "@extension/shared" import { convertAddress } from "@talisman/util/convertAddress" import { LedgerAccountDefEthereum } from "@ui/domains/Account/AccountAdd/AccountAddLedger/context" -import { getCustomTalismanLedgerError } from "@ui/hooks/ledger/errors" +import { getTalismanLedgerError } from "@ui/hooks/ledger/errors" import { useLedgerEthereum } from "@ui/hooks/ledger/useLedgerEthereum" import { AccountImportDef, useAccountImportBalances } from "@ui/hooks/useAccountImportBalances" import { useAccounts, useEvmNetworks } from "@ui/state" @@ -93,7 +93,7 @@ const useLedgerEthereumAccounts = ( message: t("Ledger is ready."), }) } catch (err) { - const error = getCustomTalismanLedgerError(err) + const error = getTalismanLedgerError(err) log.error("Failed to load page", { err }) setConnectionStatus({ status: "error", diff --git a/apps/extension/src/ui/domains/Account/LedgerSubstrateGenericAccountPicker.tsx b/apps/extension/src/ui/domains/Account/LedgerSubstrateGenericAccountPicker.tsx index 2c8109f87..b96a04d52 100644 --- a/apps/extension/src/ui/domains/Account/LedgerSubstrateGenericAccountPicker.tsx +++ b/apps/extension/src/ui/domains/Account/LedgerSubstrateGenericAccountPicker.tsx @@ -19,7 +19,7 @@ import { FormFieldContainer, FormFieldInputText, Tooltip, TooltipTrigger } from import { convertAddress } from "@talisman/util/convertAddress" import { LedgerAccountDefSubstrateGeneric } from "@ui/domains/Account/AccountAdd/AccountAddLedger/context" import { getPolkadotLedgerDerivationPath } from "@ui/hooks/ledger/common" -import { getCustomTalismanLedgerError, TalismanLedgerError } from "@ui/hooks/ledger/errors" +import { getTalismanLedgerError, TalismanLedgerError } from "@ui/hooks/ledger/errors" import { useLedgerSubstrateGeneric } from "@ui/hooks/ledger/useLedgerSubstrateGeneric" import { AccountImportDef, useAccountImportBalances } from "@ui/hooks/useAccountImportBalances" import { useAccounts, useChain, useChains } from "@ui/state" @@ -122,7 +122,7 @@ const useLedgerSubstrateGenericAccounts = ( message: t("Ledger is ready."), }) } catch (err) { - const error = getCustomTalismanLedgerError(err) + const error = getTalismanLedgerError(err) log.error("Failed to load page", { err }) setConnectionStatus({ status: "error", @@ -339,7 +339,7 @@ const useLedgerAccountAddress = ( message: t("Ledger is ready."), }) } catch (err) { - const error = getCustomTalismanLedgerError(err) + const error = getTalismanLedgerError(err) log.error("Failed to load page", { err }) setConnectionStatus({ status: "error", diff --git a/apps/extension/src/ui/domains/Account/LedgerSubstrateLegacyAccountPicker.tsx b/apps/extension/src/ui/domains/Account/LedgerSubstrateLegacyAccountPicker.tsx index fe0a098fe..97921baa7 100644 --- a/apps/extension/src/ui/domains/Account/LedgerSubstrateLegacyAccountPicker.tsx +++ b/apps/extension/src/ui/domains/Account/LedgerSubstrateLegacyAccountPicker.tsx @@ -5,7 +5,7 @@ import { useTranslation } from "react-i18next" import { isChainActive, SubstrateLedgerAppType } from "@extension/core" import { log } from "@extension/shared" import { convertAddress } from "@talisman/util/convertAddress" -import { getCustomTalismanLedgerError } from "@ui/hooks/ledger/errors" +import { getTalismanLedgerError } from "@ui/hooks/ledger/errors" import { useLedgerSubstrateLegacy } from "@ui/hooks/ledger/useLedgerSubstrateLegacy" import { AccountImportDef, useAccountImportBalances } from "@ui/hooks/useAccountImportBalances" import { useAccounts, useActiveChainsState, useChain } from "@ui/state" @@ -105,7 +105,7 @@ const useLedgerChainAccounts = ( message: t("Ledger is ready."), }) } catch (err) { - const error = getCustomTalismanLedgerError(err) + const error = getTalismanLedgerError(err) log.error("Failed to load page", { err }) setConnectionStatus({ status: "error", diff --git a/apps/extension/src/ui/domains/Sign/SignLedgerEthereum.tsx b/apps/extension/src/ui/domains/Sign/SignLedgerEthereum.tsx index 3293444cc..dc687d798 100644 --- a/apps/extension/src/ui/domains/Sign/SignLedgerEthereum.tsx +++ b/apps/extension/src/ui/domains/Sign/SignLedgerEthereum.tsx @@ -3,7 +3,7 @@ import { useTranslation } from "react-i18next" import { AccountJsonHardwareEthereum } from "@extension/core" import { log } from "@extension/shared" -import { getCustomTalismanLedgerError } from "@ui/hooks/ledger/errors" +import { getTalismanLedgerError } from "@ui/hooks/ledger/errors" import { useLedgerEthereum } from "@ui/hooks/ledger/useLedgerEthereum" import { SignHardwareEthereumProps } from "./SignHardwareEthereum" @@ -47,12 +47,12 @@ export const SignLedgerEthereum: FC = ({ const errCheck = err as Error & { statusCode?: number; reason?: string } if (errCheck.reason === "invalid object key - maxPriorityFeePerGas") { setError( - getCustomTalismanLedgerError( + getTalismanLedgerError( t("Sorry, Talisman doesn't support signing transactions with Ledger on this network."), ), ) } else { - const error = getCustomTalismanLedgerError(err) + const error = getTalismanLedgerError(err) log.error("signLedger", { error }) setError(error) } diff --git a/apps/extension/src/ui/domains/Sign/SignLedgerSubstrateGeneric.tsx b/apps/extension/src/ui/domains/Sign/SignLedgerSubstrateGeneric.tsx index 226401595..a0b384694 100644 --- a/apps/extension/src/ui/domains/Sign/SignLedgerSubstrateGeneric.tsx +++ b/apps/extension/src/ui/domains/Sign/SignLedgerSubstrateGeneric.tsx @@ -2,7 +2,7 @@ import { FC, useCallback } from "react" import { AccountJsonHardwareSubstrate } from "@extension/core" import { log } from "@extension/shared" -import { getCustomTalismanLedgerError } from "@ui/hooks/ledger/errors" +import { getTalismanLedgerError } from "@ui/hooks/ledger/errors" import { useLedgerSubstrateAppByName } from "@ui/hooks/ledger/useLedgerSubstrateApp" import { useLedgerSubstrateGeneric } from "@ui/hooks/ledger/useLedgerSubstrateGeneric" import { useAccountByAddress } from "@ui/state" @@ -44,7 +44,7 @@ export const SignLedgerSubstrateGeneric: FC = ({ // await to keep loader spinning until popup closes await onSigned({ signature }) } catch (err) { - const error = getCustomTalismanLedgerError(err) + const error = getTalismanLedgerError(err) log.error("signLedger", { error }) setError(error) } finally { diff --git a/apps/extension/src/ui/domains/Sign/SignLedgerSubstrateLegacy.tsx b/apps/extension/src/ui/domains/Sign/SignLedgerSubstrateLegacy.tsx index e018b8466..b106587d7 100644 --- a/apps/extension/src/ui/domains/Sign/SignLedgerSubstrateLegacy.tsx +++ b/apps/extension/src/ui/domains/Sign/SignLedgerSubstrateLegacy.tsx @@ -3,7 +3,7 @@ import { useTranslation } from "react-i18next" import { AccountJsonHardwareSubstrate } from "@extension/core" import { log } from "@extension/shared" -import { getCustomTalismanLedgerError } from "@ui/hooks/ledger/errors" +import { getTalismanLedgerError } from "@ui/hooks/ledger/errors" import { useLedgerSubstrateLegacy } from "@ui/hooks/ledger/useLedgerSubstrateLegacy" import { useAccountByAddress } from "@ui/state" @@ -28,7 +28,7 @@ export const SignLedgerSubstrateLegacy: FC = ({ const signWithLedger = useCallback(async () => { if (!payload || !onSigned || !account) return - if (!registry) return setError(getCustomTalismanLedgerError(t("Missing registry."))) + if (!registry) return setError(getTalismanLedgerError(t("Missing registry."))) onSentToDevice?.(true) setIsSigning(true) @@ -39,7 +39,7 @@ export const SignLedgerSubstrateLegacy: FC = ({ // await to keep loader spinning until popup closes await onSigned({ signature }) } catch (err) { - const error = getCustomTalismanLedgerError(err) + const error = getTalismanLedgerError(err) log.error("signLedger", { error }) setError(error) } finally { diff --git a/apps/extension/src/ui/hooks/ledger/errors.ts b/apps/extension/src/ui/hooks/ledger/errors.ts index 6ca628997..2bec088c0 100644 --- a/apps/extension/src/ui/hooks/ledger/errors.ts +++ b/apps/extension/src/ui/hooks/ledger/errors.ts @@ -26,6 +26,7 @@ export const getCustomNativeLedgerError = ( } type TalismanLedgerErrorName = + | "Custom" | "Unknown" | "UnsupportedVersion" | "InvalidApp" @@ -46,15 +47,14 @@ export class TalismanLedgerError extends Error { } } -export const getCustomTalismanLedgerError = (errorOrMessage: unknown): TalismanLedgerError => { - if (errorOrMessage instanceof TalismanLedgerError) return errorOrMessage +export const getTalismanLedgerError = ( + error: unknown, + appName: string = "Unknown App", +): TalismanLedgerError => { + if (error instanceof TalismanLedgerError) return error - if (typeof errorOrMessage === "string") return new TalismanLedgerError("Unknown", errorOrMessage) + if (typeof error === "string") return new TalismanLedgerError("Custom", error) - return getTalismanLedgerError(errorOrMessage as NativeLedgerError, "substrate") -} - -export const getTalismanLedgerError = (error: unknown, appName: string): TalismanLedgerError => { log.log("getTalismanLedgerError", { error }) const cause = error as NativeLedgerError @@ -115,7 +115,6 @@ export const getTalismanLedgerError = (error: unknown, appName: string): Talisma // Polkadot specific errors, wrapped in simple Error object // only message is available - // TODO Check if still a thing since ledger generic app switch (cause.message) { case "Timeout": // this one is throw by Talisman in case of timeout when calling ledger.getAddress return new TalismanLedgerError("Timeout", t("Failed to connect to your Ledger (timeout)"), { @@ -182,8 +181,7 @@ export const getTalismanLedgerError = (error: unknown, appName: string): Talisma // eslint-disable-next-line no-console DEBUG && console.warn("unmanaged ledger error", { error }) - // At this point either the error most likely not a Ledger error (custom Talisman message) or it is an error that we don't manage - // either way, we want to display the original error message + // If available, display the actual error message so our help-desk can understand what s going on return new TalismanLedgerError("Unknown", cause.message ?? "Failed to connect to your Ledger", { cause, }) diff --git a/apps/extension/src/ui/hooks/ledger/useLedgerSubstrateGeneric.ts b/apps/extension/src/ui/hooks/ledger/useLedgerSubstrateGeneric.ts index 439ec122b..3c31d5648 100644 --- a/apps/extension/src/ui/hooks/ledger/useLedgerSubstrateGeneric.ts +++ b/apps/extension/src/ui/hooks/ledger/useLedgerSubstrateGeneric.ts @@ -13,7 +13,7 @@ import { useCallback, useRef } from "react" import { useTranslation } from "react-i18next" import { getPolkadotLedgerDerivationPath } from "./common" -import { getCustomTalismanLedgerError, getTalismanLedgerError, TalismanLedgerError } from "./errors" +import { getTalismanLedgerError, TalismanLedgerError } from "./errors" import { useLedgerTransport } from "./useLedgerTransport" type LedgerRequest = (ledger: PolkadotGenericApp) => Promise @@ -93,19 +93,17 @@ const signPayload = async ( if (isJsonPayload(payload)) { if (!payload.withSignedTransaction) - throw getCustomTalismanLedgerError( + throw getTalismanLedgerError( t("This dapp needs to be updated in order to support Ledger signing."), ) - if (!registry) throw getCustomTalismanLedgerError(t("Missing registry.")) + if (!registry) throw getTalismanLedgerError(t("Missing registry.")) const hasCheckMetadataHash = registry.metadata.extrinsic.signedExtensions.some( (ext) => ext.identifier.toString() === "CheckMetadataHash", ) if (!hasCheckMetadataHash) - throw getCustomTalismanLedgerError( - t("This network doesn't support Ledger Polkadot Generic App."), - ) - if (!txMetadata) throw getCustomTalismanLedgerError(t("Missing short metadata")) + throw getTalismanLedgerError(t("This network doesn't support Ledger Polkadot Generic App.")) + if (!txMetadata) throw getTalismanLedgerError(t("Missing short metadata")) const unsigned = registry.createType("ExtrinsicPayload", payload)