-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
connection_errors.ts
112 lines (94 loc) · 3.15 KB
/
connection_errors.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
/* eslint-disable max-classes-per-file */
import { ConnectionEvent } from 'web3-types';
import {
ERR_CONN,
ERR_CONN_INVALID,
ERR_CONN_TIMEOUT,
ERR_CONN_NOT_OPEN,
ERR_CONN_CLOSE,
ERR_CONN_MAX_ATTEMPTS,
ERR_CONN_PENDING_REQUESTS,
ERR_REQ_ALREADY_SENT,
} from '../error_codes.js';
import { BaseWeb3Error } from '../web3_error_base.js';
export class ConnectionError extends BaseWeb3Error {
public code = ERR_CONN;
public errorCode?: number;
public errorReason?: string;
public constructor(message: string, event?: ConnectionEvent) {
super(message);
if (event) {
this.errorCode = event.code;
this.errorReason = event.reason;
}
}
public toJSON() {
return { ...super.toJSON(), errorCode: this.errorCode, errorReason: this.errorReason };
}
}
export class InvalidConnectionError extends ConnectionError {
public constructor(public host: string, event?: ConnectionEvent) {
super(`CONNECTION ERROR: Couldn't connect to node ${host}.`, event);
this.code = ERR_CONN_INVALID;
}
public toJSON() {
return { ...super.toJSON(), host: this.host };
}
}
export class ConnectionTimeoutError extends ConnectionError {
public constructor(public duration: number) {
super(`CONNECTION TIMEOUT: timeout of ${duration}ms achieved`);
this.code = ERR_CONN_TIMEOUT;
}
public toJSON() {
return { ...super.toJSON(), duration: this.duration };
}
}
export class ConnectionNotOpenError extends ConnectionError {
public constructor(event?: ConnectionEvent) {
super('Connection not open', event);
this.code = ERR_CONN_NOT_OPEN;
}
}
export class ConnectionCloseError extends ConnectionError {
public constructor(event?: ConnectionEvent) {
super(
`CONNECTION ERROR: The connection got closed with the close code ${
event?.code ?? ''
} and the following reason string ${event?.reason ?? ''}`,
event,
);
this.code = ERR_CONN_CLOSE;
}
}
export class MaxAttemptsReachedOnReconnectingError extends ConnectionError {
public constructor(numberOfAttempts: number) {
super(`Maximum number of reconnect attempts reached! (${numberOfAttempts})`);
this.code = ERR_CONN_MAX_ATTEMPTS;
}
}
export class PendingRequestsOnReconnectingError extends ConnectionError {
public constructor() {
super('CONNECTION ERROR: Provider started to reconnect before the response got received!');
this.code = ERR_CONN_PENDING_REQUESTS;
}
}
export class RequestAlreadySentError extends ConnectionError {
public constructor(id: number | string) {
super(`Request already sent with following id: ${id}`);
this.code = ERR_REQ_ALREADY_SENT;
}
}