From 7fb207aeac56b20a0ff4a43132d61ab85bd687ba Mon Sep 17 00:00:00 2001 From: Alessandro Candeago <54709706+alecande11@users.noreply.github.com> Date: Mon, 4 Sep 2023 15:48:49 +0200 Subject: [PATCH] ST-752: display error messages (#515) * Display error messages * Improve error parsing * Fix user denied error * fix: typos --------- Co-authored-by: Manuel Alessandro Collazo --- src/txs/Tx.tsx | 12 +++++------- src/txs/utils.ts | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/src/txs/Tx.tsx b/src/txs/Tx.tsx index 1df704a97..82859ec71 100644 --- a/src/txs/Tx.tsx +++ b/src/txs/Tx.tsx @@ -36,7 +36,7 @@ import useToPostMultisigTx from "pages/multisig/utils/useToPostMultisigTx" import { isWallet, useAuth } from "auth" import { PasswordError } from "auth/scripts/keystore" -import { toInput, CoinInput, calcTaxes } from "./utils" +import { toInput, CoinInput, calcTaxes, parseError } from "./utils" import styles from "./Tx.module.scss" import { useInterchainLCDClient } from "data/queries/lcdClient" import { useInterchainAddresses } from "auth/hooks/useAddress" @@ -232,7 +232,7 @@ function Tx(props: Props) { : props.disabled || "" const [submitting, setSubmitting] = useState(false) - const [error, setError] = useState() + const [error, setError] = useState() const navigate = useNavigate() const toPostMultisigTx = useToPostMultisigTx() @@ -464,12 +464,10 @@ function Tx(props: Props) { const modal = !error ? undefined : { - title: error?.toString().includes("UserDenied") - ? t("Transaction was denied by user") - : t("Error"), - children: error?.toString().includes("UserDenied") ? null : ( + title: t(parseError(error).title), + children: (
-            {error?.message}
+            {t(parseError(error).message)}
           
), } diff --git a/src/txs/utils.ts b/src/txs/utils.ts index 99e22e4f9..be6393d04 100644 --- a/src/txs/utils.ts +++ b/src/txs/utils.ts @@ -65,3 +65,47 @@ export const calcTaxes = ( }) ) } + +export function parseError(error: any): { title: string; message: string } { + if (error instanceof Error || typeof error.message === "string") { + const { message, name } = error + + switch (message) { + case "Ledger device: UNKNOWN_ERROR (0x5515)": + return { + title: "Ledger error", + message: + "Please make sure your Ledger device is connected and unlocked.", + } + case "Unknown Status Code: 28161": + return { + title: "Ledger error", + message: + "Please make sure you have the Terra and Cosmos apps installed on your Ledger device.", + } + case "Failed to execute 'claimInterface' on 'USBDevice': Unable to claim interface.": + return { + title: "Ledger error", + message: + "The Ledger device is being used on another tab, please close all the other browser windows and try again.", + } + default: + return { + title: name?.includes("Ledger") ? "Ledger error" : "Error", + message, + } + } + } else if (typeof error === "string") { + return { + title: "Error", + message: error.includes("User denied") + ? "Transaction was denied by user." + : error, + } + } else { + return { + title: "Error", + message: JSON.stringify(error.error ?? error, null, 2), + } + } +}