0
# Error Handling
1
2
Complete error handling system providing standardized error creation for connection issues, validation failures, transaction problems, and contract interactions. All error functions return Error objects with additional metadata properties.
3
4
## Capabilities
5
6
### Connection & Provider Errors
7
8
Errors related to provider connections and network issues.
9
10
```javascript { .api }
11
/**
12
* Creates error from JSON-RPC error response
13
* @param result - Error result from JSON-RPC response
14
* @returns Error with formatted message and data property
15
*/
16
function ErrorResponse(result: Error): Error;
17
18
/**
19
* Creates connection failure error
20
* @param host - Host that failed to connect
21
* @param event - Optional WebSocket event details
22
* @returns ConnectionError with code and reason properties
23
*/
24
function InvalidConnection(host: string, event?: WebSocketEvent): ConnectionError;
25
26
/**
27
* Creates invalid provider error
28
* @returns Error indicating provider is not set or invalid
29
*/
30
function InvalidProvider(): Error;
31
32
/**
33
* Creates invalid JSON-RPC response error
34
* @param result - Invalid response that caused the error
35
* @returns Error with formatted message
36
*/
37
function InvalidResponse(result: Error): Error;
38
39
/**
40
* Creates connection timeout error
41
* @param ms - Timeout duration that was exceeded
42
* @returns Error indicating connection timeout
43
*/
44
function ConnectionTimeout(ms: string): Error;
45
46
/**
47
* Creates connection not open error
48
* @returns Error indicating connection is not open for sending
49
*/
50
function ConnectionNotOpenError(): Error;
51
52
/**
53
* Creates connection close error
54
* @param event - WebSocket close event or boolean
55
* @returns Error or ConnectionError with close details
56
*/
57
function ConnectionCloseError(event: WebSocketEvent | boolean): Error | ConnectionError;
58
59
/**
60
* Creates max reconnection attempts error
61
* @returns Error indicating maximum reconnection attempts reached
62
*/
63
function MaxAttemptsReachedOnReconnectingError(): Error;
64
65
/**
66
* Creates pending requests during reconnection error
67
* @returns Error indicating reconnection started with pending requests
68
*/
69
function PendingRequestsOnReconnectingError(): Error;
70
71
/**
72
* Creates generic connection error
73
* @param msg - Error message
74
* @param event - Optional WebSocket event details
75
* @returns ConnectionError with code and reason from event
76
*/
77
function ConnectionError(msg: string, event?: WebSocketEvent): ConnectionError;
78
```
79
80
**Usage Examples:**
81
82
```javascript
83
const { errors } = require("web3-core-helpers");
84
85
// Connection failures
86
const connError = errors.InvalidConnection("wss://localhost:8546");
87
console.log(connError.message); // "CONNECTION ERROR: Couldn't connect to node wss://localhost:8546."
88
89
// Timeout errors
90
const timeoutError = errors.ConnectionTimeout("30000");
91
console.log(timeoutError.message); // "CONNECTION TIMEOUT: timeout of 30000 ms achived"
92
93
// Provider issues
94
const providerError = errors.InvalidProvider();
95
console.log(providerError.message); // "Provider not set or invalid"
96
```
97
98
### Parameter Validation Errors
99
100
Errors for parameter count and validation issues.
101
102
```javascript { .api }
103
/**
104
* Creates parameter count validation error
105
* @param got - Number of parameters received
106
* @param expected - Number of parameters expected
107
* @param method - Method name that failed validation
108
* @returns Error with parameter count details
109
*/
110
function InvalidNumberOfParams(got: number, expected: number, method: string): Error;
111
```
112
113
**Usage Example:**
114
115
```javascript
116
const paramError = errors.InvalidNumberOfParams(2, 3, "eth_getBalance");
117
console.log(paramError.message); // 'Invalid number of parameters for "eth_getBalance". Got 2 expected 3!'
118
```
119
120
### Transaction & Smart Contract Errors
121
122
Errors related to transaction execution and smart contract interactions.
123
124
```javascript { .api }
125
/**
126
* Creates smart contract revert error
127
* @param reason - Revert reason string
128
* @param signature - Method signature that reverted
129
* @returns RevertInstructionError with reason and signature properties
130
*/
131
function RevertInstructionError(reason: string, signature: string): RevertInstructionError;
132
133
/**
134
* Creates transaction revert error with receipt
135
* @param reason - Revert reason string
136
* @param signature - Method signature that reverted
137
* @param receipt - Transaction receipt object
138
* @returns TransactionRevertInstructionError with reason, signature, and receipt
139
*/
140
function TransactionRevertInstructionError(reason: string, signature: string, receipt: object): TransactionRevertInstructionError;
141
142
/**
143
* Creates generic transaction error
144
* @param message - Error message
145
* @param receipt - Transaction receipt object
146
* @returns TransactionError with receipt property
147
*/
148
function TransactionError(message: string, receipt: object): TransactionError;
149
150
/**
151
* Creates no contract address found error
152
* @param receipt - Transaction receipt without contract address
153
* @returns TransactionError indicating missing contract address
154
*/
155
function NoContractAddressFoundError(receipt: object): TransactionError;
156
157
/**
158
* Creates contract code not stored error
159
* @param receipt - Transaction receipt from failed deployment
160
* @returns TransactionError indicating code storage failure
161
*/
162
function ContractCodeNotStoredError(receipt: object): TransactionError;
163
164
/**
165
* Creates transaction reverted without reason error
166
* @param receipt - Transaction receipt from reverted transaction
167
* @returns TransactionError for revert without reason string
168
*/
169
function TransactionRevertedWithoutReasonError(receipt: object): TransactionError;
170
171
/**
172
* Creates out of gas transaction error
173
* @param receipt - Transaction receipt from out-of-gas transaction
174
* @returns TransactionError indicating insufficient gas
175
*/
176
function TransactionOutOfGasError(receipt: object): TransactionError;
177
```
178
179
**Usage Examples:**
180
181
```javascript
182
// Smart contract revert
183
const revertError = errors.RevertInstructionError("Insufficient balance", "transfer(address,uint256)");
184
console.log(revertError.reason); // "Insufficient balance"
185
console.log(revertError.signature); // "transfer(address,uint256)"
186
187
// Transaction errors with receipt
188
const receipt = { transactionHash: "0x...", gasUsed: "0x5208" };
189
const gasError = errors.TransactionOutOfGasError(receipt);
190
console.log(gasError.receipt); // Contains the receipt object
191
```
192
193
### Contract-Specific Errors
194
195
Errors specific to contract instantiation and interaction.
196
197
```javascript { .api }
198
/**
199
* Creates ENS resolver method missing error
200
* @param address - Resolver contract address
201
* @param name - Missing method name
202
* @returns Error indicating resolver method not implemented
203
*/
204
function ResolverMethodMissingError(address: string, name: string): Error;
205
206
/**
207
* Creates contract missing ABI error
208
* @returns Error indicating contract ABI not provided
209
*/
210
function ContractMissingABIError(): Error;
211
212
/**
213
* Creates contract once requires callback error
214
* @returns Error indicating missing callback for once method
215
*/
216
function ContractOnceRequiresCallbackError(): Error;
217
218
/**
219
* Creates contract event does not exist error
220
* @param eventName - Name of non-existent event
221
* @returns Error indicating event not found in contract
222
*/
223
function ContractEventDoesNotExistError(eventName: string): Error;
224
225
/**
226
* Creates contract reserved event error
227
* @param type - Reserved event name that was used
228
* @returns Error indicating reserved event name usage
229
*/
230
function ContractReservedEventError(type: string): Error;
231
232
/**
233
* Creates contract missing deploy data error
234
* @returns Error indicating missing deployment data
235
*/
236
function ContractMissingDeployDataError(): Error;
237
238
/**
239
* Creates contract no address defined error
240
* @returns Error indicating contract address not set
241
*/
242
function ContractNoAddressDefinedError(): Error;
243
244
/**
245
* Creates contract no from address defined error
246
* @returns Error indicating missing from address
247
*/
248
function ContractNoFromAddressDefinedError(): Error;
249
```
250
251
**Usage Examples:**
252
253
```javascript
254
// Contract ABI missing
255
const abiError = errors.ContractMissingABIError();
256
console.log(abiError.message); // "You must provide the json interface of the contract when instantiating a contract object."
257
258
// Event not found
259
const eventError = errors.ContractEventDoesNotExistError("NonExistentEvent");
260
console.log(eventError.message); // 'Event "NonExistentEvent" doesn\'t exist in this contract.'
261
262
// Reserved event name
263
const reservedError = errors.ContractReservedEventError("newListener");
264
console.log(reservedError.message); // 'The event "newListener" is a reserved event name, you can\'t use it.'
265
```
266
267
## Types
268
269
```typescript { .api }
270
interface ConnectionError extends Error {
271
code: string | undefined;
272
reason: string | undefined;
273
}
274
275
interface TransactionError extends Error {
276
receipt: object;
277
}
278
279
interface RevertInstructionError extends Error {
280
reason: string;
281
signature: string;
282
}
283
284
interface TransactionRevertInstructionError extends Error {
285
reason: string;
286
signature: string;
287
receipt: object;
288
}
289
290
interface WebSocketEvent {
291
code?: number;
292
reason?: string;
293
}
294
```