Skip to content

Commit

Permalink
refactor: single method for wrapping ledger errors
Browse files Browse the repository at this point in the history
  • Loading branch information
0xKheops committed Jan 6, 2025
1 parent 4ca65c1 commit 520f3d7
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -51,7 +51,7 @@ export const ConnectLedgerBase: FC<ConnectLedgerBaseProps> = ({
})
onReadyChanged?.(true)
} catch (err) {
const error = getCustomTalismanLedgerError(err)
const error = getTalismanLedgerError(err)
log.error("ConnectLedgerSubstrateGeneric", { error })
setConnectionStatus({
status: "error",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions apps/extension/src/ui/domains/Sign/SignLedgerEthereum.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -47,12 +47,12 @@ export const SignLedgerEthereum: FC<SignHardwareEthereumProps> = ({
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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -44,7 +44,7 @@ export const SignLedgerSubstrateGeneric: FC<SignHardwareSubstrateProps> = ({
// 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -28,7 +28,7 @@ export const SignLedgerSubstrateLegacy: FC<SignHardwareSubstrateProps> = ({

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)
Expand All @@ -39,7 +39,7 @@ export const SignLedgerSubstrateLegacy: FC<SignHardwareSubstrateProps> = ({
// 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 {
Expand Down
18 changes: 8 additions & 10 deletions apps/extension/src/ui/hooks/ledger/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const getCustomNativeLedgerError = (
}

type TalismanLedgerErrorName =
| "Custom"
| "Unknown"
| "UnsupportedVersion"
| "InvalidApp"
Expand All @@ -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
Expand Down Expand Up @@ -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)"), {
Expand Down Expand Up @@ -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,
})
Expand Down
12 changes: 5 additions & 7 deletions apps/extension/src/ui/hooks/ledger/useLedgerSubstrateGeneric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> = (ledger: PolkadotGenericApp) => Promise<T>
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 520f3d7

Please sign in to comment.