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