File tree Expand file tree Collapse file tree 3 files changed +25
-4
lines changed Expand file tree Collapse file tree 3 files changed +25
-4
lines changed Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change 11import { toast , ToastPosition , Theme } from "react-toastify" ;
22import { type PublicClient , type TransactionReceipt } from "viem" ;
33
4+ import { parseWagmiError } from "./parseWagmiError" ;
5+
46export 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
4345export 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}
Original file line number Diff line number Diff line change 11import { toast , ToastPosition , Theme } from "react-toastify" ;
22import { PublicClient , TransactionReceipt } from "viem" ;
33
4+ import { parseWagmiError } from "./parseWagmiError" ;
5+
46export 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
4749export 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}
You can’t perform that action at this time.
0 commit comments