Skip to content

Commit 5f5771d

Browse files
committed
chore: use-parse-wagmi-error-function
1 parent e1e44e4 commit 5f5771d

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { type SimulateContractErrorType } from "@wagmi/core";
2+
3+
type ExtendedWagmiError = SimulateContractErrorType & { shortMessage?: string; metaMessages?: string[] };
4+
5+
/**
6+
* @param error
7+
* @description Tries to extract the human readable error message, otherwise reverts to error.message
8+
* @returns Human readable error if possible
9+
*/
10+
export const parseWagmiError = (error: SimulateContractErrorType) => {
11+
const extError = error as ExtendedWagmiError;
12+
13+
const metaMessage = extError?.metaMessages?.[0];
14+
const shortMessage = extError?.shortMessage;
15+
16+
return metaMessage ?? shortMessage ?? error.message;
17+
};

web-devtools/src/utils/wrapWithToast.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { toast, ToastPosition, Theme } from "react-toastify";
22
import { type PublicClient, type TransactionReceipt } from "viem";
33

4+
import { parseWagmiError } from "./parseWagmiError";
5+
46
export const OPTIONS = {
57
position: "top-center" as ToastPosition,
68
autoClose: 5000,
@@ -35,11 +37,11 @@ export async function wrapWithToast(
3537
})
3638
)
3739
.catch((error) => {
38-
toast.error(error.shortMessage ?? error.message, OPTIONS);
40+
toast.error(parseWagmiError(error), OPTIONS);
3941
return { status: false };
4042
});
4143
}
4244

4345
export async function catchShortMessage(promise: Promise<any>) {
44-
return await promise.catch((error) => toast.error(error.shortMessage ?? error.message, OPTIONS));
46+
return await promise.catch((error) => toast.error(parseWagmiError(error), OPTIONS));
4547
}

web/src/utils/wrapWithToast.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { toast, ToastPosition, Theme } from "react-toastify";
22
import { PublicClient, TransactionReceipt } from "viem";
33

4+
import { parseWagmiError } from "./parseWagmiError";
5+
46
export const OPTIONS = {
57
position: "top-center" as ToastPosition,
68
autoClose: 5000,
@@ -39,11 +41,11 @@ export async function wrapWithToast(
3941
})
4042
)
4143
.catch((error) => {
42-
toast.error(error.shortMessage ?? error.message, OPTIONS);
44+
toast.error(parseWagmiError(error), OPTIONS);
4345
return { status: false };
4446
});
4547
}
4648

4749
export async function catchShortMessage(promise: Promise<any>) {
48-
return await promise.catch((error) => toast.error(error.shortMessage ?? error.message, OPTIONS));
50+
return await promise.catch((error) => toast.error(parseWagmiError(error), OPTIONS));
4951
}

0 commit comments

Comments
 (0)