1
- import { CustomError , WalletConnectError , getErrorContext , handleTezError } from "./ErrorContext" ;
1
+ import { TezosOperationError , type TezosOperationErrorWithMessage } from "@taquito/taquito" ;
2
+
3
+ import {
4
+ CustomError ,
5
+ WalletConnectError ,
6
+ getTezErrorMessage ,
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,114 @@ 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 ( "getTezErrorMessage " , ( ) => {
69
77
it ( "catches subtraction_underflow" , ( ) => {
70
- const res = handleTezError ( new Error ( "subtraction_underflow" ) ) ;
78
+ const res = getTezErrorMessage ( "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 = getTezErrorMessage ( "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 = getTezErrorMessage ( "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 = getTezErrorMessage ( "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 = getTezErrorMessage ( "delegate.unchanged" ) ;
93
101
expect ( res ) . toBe ( "The delegate is unchanged. Delegation to this address is already done." ) ;
94
102
} ) ;
95
103
104
+ it ( "catches contract.manager.unregistered_delegate" , ( ) => {
105
+ const res = getTezErrorMessage ( "contract.manager.unregistered_delegate" ) ;
106
+ expect ( res ) . toBe (
107
+ "The provided delegate address is not registered as a delegate. Verify the delegate address and ensure it is active."
108
+ ) ;
109
+ } ) ;
110
+
96
111
it ( "returns undefined for unknown errors" , ( ) => {
97
- const err = new Error ( "unknown error" ) ;
98
- expect ( handleTezError ( err ) ) . toBeUndefined ( ) ;
112
+ const err = "unknown error" ;
113
+ expect ( getTezErrorMessage ( err ) ) . toBeUndefined ( ) ;
114
+ } ) ;
115
+
116
+ it ( "should return default error message for unknown error" , ( ) => {
117
+ const error = new Error ( "Unknown error" ) ;
118
+ const context = getErrorContext ( error ) ;
119
+ expect ( context . description ) . toBe (
120
+ "Something went wrong. Please try again. Contact support if the issue persists. Details: Unknown error"
121
+ ) ;
122
+ } ) ;
123
+
124
+ it ( "should return custom error message for CustomError" , ( ) => {
125
+ const error = new CustomError ( "Custom error message" ) ;
126
+ const context = getErrorContext ( error ) ;
127
+ expect ( context . description ) . toBe ( "Custom error message" ) ;
128
+ } ) ;
129
+
130
+ it ( "should return WalletConnectError message" , ( ) => {
131
+ const error = new WalletConnectError ( "WC error custom text" , "INTERNAL_ERROR" , null ) ;
132
+ const context = getErrorContext ( error ) ;
133
+ expect ( context . description ) . toBe ( "WC error custom text" ) ;
134
+ expect ( context . code ) . toBe ( 4011 ) ;
135
+ expect ( context . technicalDetails ) . toBeUndefined ( ) ;
136
+ } ) ;
137
+
138
+ it ( "should return TezosOperationError message" , ( ) => {
139
+ // const error = new TezosOperationError(errors:[], lastError: { id: 'michelson_v1.script_rejected', with: { prim: 'Unit' } });
140
+ const mockError : TezosOperationErrorWithMessage = {
141
+ kind : "temporary" ,
142
+ id : "proto.020-PsParisC.michelson_v1.script_rejected" ,
143
+ with : { string : "Fail entrypoint" } , // Include the `with` field for testing
144
+ } ;
145
+ const error = new TezosOperationError (
146
+ [ mockError ] ,
147
+ "Operation failed due to a rejected script." ,
148
+ [ ]
149
+ ) ;
150
+ const context = getErrorContext ( error ) ;
151
+ expect ( context . description ) . toContain (
152
+ "Rejected by chain. The contract code failed to run. Please check the contract. Details: Fail entrypoint"
153
+ ) ;
154
+ expect ( context . technicalDetails ) . toEqual ( [
155
+ "proto.020-PsParisC.michelson_v1.script_rejected" ,
156
+ { with : { string : "Fail entrypoint" } } ,
157
+ ] ) ;
158
+ } ) ;
159
+
160
+ it ( "should return error response for getWcErrorResponse" , ( ) => {
161
+ const error = new Error ( "Unknown error" ) ;
162
+ const response = getWcErrorResponse ( error ) ;
163
+ expect ( response . message ) . toBe (
164
+ "Something went wrong. Please try again. Contact support if the issue persists. Details: Unknown error"
165
+ ) ;
166
+ expect ( response . code ) . toBe ( 4011 ) ;
167
+ expect ( response . data ) . toBe ( "Unknown error" ) ;
99
168
} ) ;
100
169
} ) ;
0 commit comments