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 1
1
import { toast , ToastPosition , Theme } from "react-toastify" ;
2
2
import { type PublicClient , type TransactionReceipt } from "viem" ;
3
3
4
+ import { parseWagmiError } from "./parseWagmiError" ;
5
+
4
6
export const OPTIONS = {
5
7
position : "top-center" as ToastPosition ,
6
8
autoClose : 5000 ,
@@ -35,11 +37,11 @@ export async function wrapWithToast(
35
37
} )
36
38
)
37
39
. catch ( ( error ) => {
38
- toast . error ( error . shortMessage ?? error . message , OPTIONS ) ;
40
+ toast . error ( parseWagmiError ( error ) , OPTIONS ) ;
39
41
return { status : false } ;
40
42
} ) ;
41
43
}
42
44
43
45
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 ) ) ;
45
47
}
Original file line number Diff line number Diff line change 1
1
import { toast , ToastPosition , Theme } from "react-toastify" ;
2
2
import { PublicClient , TransactionReceipt } from "viem" ;
3
3
4
+ import { parseWagmiError } from "./parseWagmiError" ;
5
+
4
6
export const OPTIONS = {
5
7
position : "top-center" as ToastPosition ,
6
8
autoClose : 5000 ,
@@ -39,11 +41,11 @@ export async function wrapWithToast(
39
41
} )
40
42
)
41
43
. catch ( ( error ) => {
42
- toast . error ( error . shortMessage ?? error . message , OPTIONS ) ;
44
+ toast . error ( parseWagmiError ( error ) , OPTIONS ) ;
43
45
return { status : false } ;
44
46
} ) ;
45
47
}
46
48
47
49
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 ) ) ;
49
51
}
You can’t perform that action at this time.
0 commit comments