1
- import { CustomError , WalletConnectError , getErrorContext , handleTezError } from "./ErrorContext" ;
1
+ import { TezosOperationError , type TezosOperationErrorWithMessage } from "@taquito/taquito" ;
2
+
3
+ import {
4
+ CustomError ,
5
+ WalletConnectError ,
6
+ explainTezError ,
7
+ getErrorContext ,
8
+ getWcErrorResponse ,
9
+ } from "./ErrorContext" ;
2
10
3
11
describe ( "getErrorContext" , ( ) => {
4
12
it ( "should handle error object with message and stack" , ( ) => {
@@ -12,7 +20,7 @@ describe("getErrorContext", () => {
12
20
expect ( context . technicalDetails ) . toBe ( "some error message" ) ;
13
21
expect ( context . stacktrace ) . toBe ( "some stacktrace" ) ;
14
22
expect ( context . description ) . toBe (
15
- "Something went wrong. Please try again or contact support if the issue persists."
23
+ "Something went wrong. Please try again. Contact support if the issue persists. Details: some error message "
16
24
) ;
17
25
expect ( context . timestamp ) . toBeDefined ( ) ;
18
26
} ) ;
@@ -25,7 +33,7 @@ describe("getErrorContext", () => {
25
33
expect ( context . technicalDetails ) . toBe ( "string error message" ) ;
26
34
expect ( context . stacktrace ) . toBe ( "" ) ;
27
35
expect ( context . description ) . toBe (
28
- "Something went wrong. Please try again or contact support if the issue persists."
36
+ "Something went wrong. Please try again. Contact support if the issue persists."
29
37
) ;
30
38
expect ( context . timestamp ) . toBeDefined ( ) ;
31
39
} ) ;
@@ -48,53 +56,107 @@ describe("getErrorContext", () => {
48
56
49
57
const context = getErrorContext ( error ) ;
50
58
51
- expect ( context . technicalDetails ) . toBe ( "" ) ;
59
+ expect ( context . technicalDetails ) . toBeUndefined ( ) ;
52
60
expect ( context . description ) . toBe ( "Custom error message" ) ;
53
61
expect ( context . stacktrace ) . toBeDefined ( ) ;
54
62
expect ( context . timestamp ) . toBeDefined ( ) ;
55
63
} ) ;
56
64
it ( "should handle WalletConnectError instances" , ( ) => {
57
- const error = new WalletConnectError ( "Custom WC error message" , "UNSUPPORTED_EVENTS " , null ) ;
65
+ const error = new WalletConnectError ( "Custom WC error message" , "INTERNAL_ERROR " , null ) ;
58
66
59
67
const context = getErrorContext ( error ) ;
60
68
61
- expect ( context . technicalDetails ) . toBe ( "" ) ;
69
+ expect ( context . technicalDetails ) . toBeUndefined ( ) ;
62
70
expect ( context . description ) . toBe ( "Custom WC error message" ) ;
63
71
expect ( context . stacktrace ) . toBeDefined ( ) ;
64
72
expect ( context . timestamp ) . toBeDefined ( ) ;
65
73
} ) ;
66
74
} ) ;
67
75
68
- describe ( "handleTezError " , ( ) => {
76
+ describe ( "explainTezError " , ( ) => {
69
77
it ( "catches subtraction_underflow" , ( ) => {
70
- const res = handleTezError ( new Error ( "subtraction_underflow" ) ) ;
78
+ const res = explainTezError ( "subtraction_underflow" ) ;
71
79
expect ( res ) . toBe ( "Insufficient balance, please make sure you have enough funds." ) ;
72
80
} ) ;
73
81
74
82
it ( "catches non_existing_contract" , ( ) => {
75
- const res = handleTezError ( new Error ( "contract.non_existing_contract" ) ) ;
83
+ const res = explainTezError ( "contract.non_existing_contract" ) ;
76
84
expect ( res ) . toBe ( "Contract does not exist, please check if the correct network is selected." ) ;
77
85
} ) ;
78
86
79
87
it ( "catches staking_to_delegate_that_refuses_external_staking" , ( ) => {
80
- const res = handleTezError ( new Error ( "staking_to_delegate_that_refuses_external_staking" ) ) ;
88
+ const res = explainTezError ( "staking_to_delegate_that_refuses_external_staking" ) ;
81
89
expect ( res ) . toBe ( "The baker you are trying to stake to does not accept external staking." ) ;
82
90
} ) ;
83
91
84
92
it ( "catches empty_implicit_delegated_contract" , ( ) => {
85
- const res = handleTezError ( new Error ( "empty_implicit_delegated_contract" ) ) ;
93
+ const res = explainTezError ( "empty_implicit_delegated_contract" ) ;
86
94
expect ( res ) . toBe (
87
95
"Emptying an implicit delegated account is not allowed. End delegation before trying again."
88
96
) ;
89
97
} ) ;
90
98
91
99
it ( "catches delegate.unchanged" , ( ) => {
92
- const res = handleTezError ( new Error ( "delegate.unchanged" ) ) ;
100
+ const res = explainTezError ( "delegate.unchanged" ) ;
93
101
expect ( res ) . toBe ( "The delegate is unchanged. Delegation to this address is already done." ) ;
94
102
} ) ;
95
103
96
104
it ( "returns undefined for unknown errors" , ( ) => {
97
- const err = new Error ( "unknown error" ) ;
98
- expect ( handleTezError ( err ) ) . toBeUndefined ( ) ;
105
+ const err = "unknown error" ;
106
+ expect ( explainTezError ( err ) ) . toBeUndefined ( ) ;
107
+ } ) ;
108
+
109
+ it ( "should return default error message for unknown error" , ( ) => {
110
+ const error = new Error ( "Unknown error" ) ;
111
+ const context = getErrorContext ( error ) ;
112
+ expect ( context . description ) . toBe (
113
+ "Something went wrong. Please try again. Contact support if the issue persists. Details: Unknown error"
114
+ ) ;
115
+ } ) ;
116
+
117
+ it ( "should return custom error message for CustomError" , ( ) => {
118
+ const error = new CustomError ( "Custom error message" ) ;
119
+ const context = getErrorContext ( error ) ;
120
+ expect ( context . description ) . toBe ( "Custom error message" ) ;
121
+ } ) ;
122
+
123
+ it ( "should return WalletConnectError message" , ( ) => {
124
+ const error = new WalletConnectError ( "WC error custom text" , "INTERNAL_ERROR" , null ) ;
125
+ const context = getErrorContext ( error ) ;
126
+ expect ( context . description ) . toBe ( "WC error custom text" ) ;
127
+ expect ( context . code ) . toBe ( 4011 ) ;
128
+ expect ( context . technicalDetails ) . toBeUndefined ( ) ;
129
+ } ) ;
130
+
131
+ it ( "should return TezosOperationError message" , ( ) => {
132
+ // const error = new TezosOperationError(errors:[], lastError: { id: 'michelson_v1.script_rejected', with: { prim: 'Unit' } });
133
+ const mockError : TezosOperationErrorWithMessage = {
134
+ kind : "temporary" ,
135
+ id : "proto.020-PsParisC.michelson_v1.script_rejected" ,
136
+ with : { string : "Fail entrypoint" } , // Include the `with` field for testing
137
+ } ;
138
+ const error = new TezosOperationError (
139
+ [ mockError ] ,
140
+ "Operation failed due to a rejected script." ,
141
+ [ ]
142
+ ) ;
143
+ const context = getErrorContext ( error ) ;
144
+ expect ( context . description ) . toContain (
145
+ "Rejected by chain. The contract code failed to run. Please check the contract. Details: Fail entrypoint"
146
+ ) ;
147
+ expect ( context . technicalDetails ) . toEqual ( [
148
+ "proto.020-PsParisC.michelson_v1.script_rejected" ,
149
+ { with : { string : "Fail entrypoint" } } ,
150
+ ] ) ;
151
+ } ) ;
152
+
153
+ it ( "should return error response for getWcErrorResponse" , ( ) => {
154
+ const error = new Error ( "Unknown error" ) ;
155
+ const response = getWcErrorResponse ( error ) ;
156
+ expect ( response . message ) . toBe (
157
+ "Something went wrong. Please try again. Contact support if the issue persists. Details: Unknown error"
158
+ ) ;
159
+ expect ( response . code ) . toBe ( 4011 ) ;
160
+ expect ( response . data ) . toBe ( "Unknown error" ) ;
99
161
} ) ;
100
162
} ) ;
0 commit comments