-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
rpc_errors.ts
197 lines (175 loc) · 6.31 KB
/
rpc_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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
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 { JsonRpcResponseWithError, JsonRpcId, JsonRpcError } from 'web3-types';
import { BaseWeb3Error } from '../web3_error_base.js';
import {
ERR_RPC_INTERNAL_ERROR,
ERR_RPC_INVALID_INPUT,
ERR_RPC_INVALID_JSON,
ERR_RPC_INVALID_METHOD,
ERR_RPC_INVALID_PARAMS,
ERR_RPC_INVALID_REQUEST,
ERR_RPC_LIMIT_EXCEEDED,
ERR_RPC_MISSING_RESOURCE,
ERR_RPC_NOT_SUPPORTED,
ERR_RPC_TRANSACTION_REJECTED,
ERR_RPC_UNAVAILABLE_RESOURCE,
ERR_RPC_UNSUPPORTED_METHOD,
} from '../error_codes.js';
import { RpcErrorMessages, genericRpcErrorMessageTemplate } from './rpc_error_messages.js';
export class RpcError extends BaseWeb3Error {
public code: number;
public id: JsonRpcId;
public jsonrpc: string;
public jsonRpcError: JsonRpcError;
public constructor(rpcError: JsonRpcResponseWithError, message?: string) {
super(
message ??
genericRpcErrorMessageTemplate.replace('*code*', rpcError.error.code.toString()),
);
this.code = rpcError.error.code;
this.id = rpcError.id;
this.jsonrpc = rpcError.jsonrpc;
this.jsonRpcError = rpcError.error;
}
public toJSON() {
return { ...super.toJSON(), error: this.jsonRpcError, id: this.id, jsonRpc: this.jsonrpc };
}
}
export class EIP1193ProviderRpcError extends BaseWeb3Error {
public code: number;
public data?: unknown;
public constructor(code: number, data?: unknown) {
if (!code) {
// this case should ideally not happen
super();
} else if (RpcErrorMessages[code]?.message) {
super(RpcErrorMessages[code].message);
} else {
// Retrieve the status code object for the given code from the table, by searching through the appropriate range
const statusCodeRange = Object.keys(RpcErrorMessages).find(
statusCode =>
typeof statusCode === 'string' &&
code >= parseInt(statusCode.split('-')[0], 10) &&
code <= parseInt(statusCode.split('-')[1], 10),
);
super(
RpcErrorMessages[statusCodeRange ?? '']?.message ??
genericRpcErrorMessageTemplate.replace('*code*', code?.toString() ?? '""'),
);
}
this.code = code;
this.data = data;
}
}
export class ParseError extends RpcError {
public code = ERR_RPC_INVALID_JSON;
public constructor(rpcError: JsonRpcResponseWithError) {
super(rpcError, RpcErrorMessages[ERR_RPC_INVALID_JSON].message);
}
}
export class InvalidRequestError extends RpcError {
public code = ERR_RPC_INVALID_REQUEST;
public constructor(rpcError: JsonRpcResponseWithError) {
super(rpcError, RpcErrorMessages[ERR_RPC_INVALID_REQUEST].message);
}
}
export class MethodNotFoundError extends RpcError {
public code = ERR_RPC_INVALID_METHOD;
public constructor(rpcError: JsonRpcResponseWithError) {
super(rpcError, RpcErrorMessages[ERR_RPC_INVALID_METHOD].message);
}
}
export class InvalidParamsError extends RpcError {
public code = ERR_RPC_INVALID_PARAMS;
public constructor(rpcError: JsonRpcResponseWithError) {
super(rpcError, RpcErrorMessages[ERR_RPC_INVALID_PARAMS].message);
}
}
export class InternalError extends RpcError {
public code = ERR_RPC_INTERNAL_ERROR;
public constructor(rpcError: JsonRpcResponseWithError) {
super(rpcError, RpcErrorMessages[ERR_RPC_INTERNAL_ERROR].message);
}
}
export class InvalidInputError extends RpcError {
public code = ERR_RPC_INVALID_INPUT;
public constructor(rpcError: JsonRpcResponseWithError) {
super(rpcError, RpcErrorMessages[ERR_RPC_INVALID_INPUT].message);
}
}
export class MethodNotSupported extends RpcError {
public code = ERR_RPC_UNSUPPORTED_METHOD;
public constructor(rpcError: JsonRpcResponseWithError) {
super(rpcError, RpcErrorMessages[ERR_RPC_UNSUPPORTED_METHOD].message);
}
}
export class ResourceUnavailableError extends RpcError {
public code = ERR_RPC_UNAVAILABLE_RESOURCE;
public constructor(rpcError: JsonRpcResponseWithError) {
super(rpcError, RpcErrorMessages[ERR_RPC_UNAVAILABLE_RESOURCE].message);
}
}
export class ResourcesNotFoundError extends RpcError {
public code = ERR_RPC_MISSING_RESOURCE;
public constructor(rpcError: JsonRpcResponseWithError) {
super(rpcError, RpcErrorMessages[ERR_RPC_MISSING_RESOURCE].message);
}
}
export class VersionNotSupportedError extends RpcError {
public code = ERR_RPC_NOT_SUPPORTED;
public constructor(rpcError: JsonRpcResponseWithError) {
super(rpcError, RpcErrorMessages[ERR_RPC_NOT_SUPPORTED].message);
}
}
export class TransactionRejectedError extends RpcError {
public code = ERR_RPC_TRANSACTION_REJECTED;
public constructor(rpcError: JsonRpcResponseWithError) {
super(rpcError, RpcErrorMessages[ERR_RPC_TRANSACTION_REJECTED].message);
}
}
export class LimitExceededError extends RpcError {
public code = ERR_RPC_LIMIT_EXCEEDED;
public constructor(rpcError: JsonRpcResponseWithError) {
super(rpcError, RpcErrorMessages[ERR_RPC_LIMIT_EXCEEDED].message);
}
}
export const rpcErrorsMap = new Map<number, { error: typeof RpcError }>();
rpcErrorsMap.set(ERR_RPC_INVALID_JSON, { error: ParseError });
rpcErrorsMap.set(ERR_RPC_INVALID_REQUEST, {
error: InvalidRequestError,
});
rpcErrorsMap.set(ERR_RPC_INVALID_METHOD, {
error: MethodNotFoundError,
});
rpcErrorsMap.set(ERR_RPC_INVALID_PARAMS, { error: InvalidParamsError });
rpcErrorsMap.set(ERR_RPC_INTERNAL_ERROR, { error: InternalError });
rpcErrorsMap.set(ERR_RPC_INVALID_INPUT, { error: InvalidInputError });
rpcErrorsMap.set(ERR_RPC_UNSUPPORTED_METHOD, {
error: MethodNotSupported,
});
rpcErrorsMap.set(ERR_RPC_UNAVAILABLE_RESOURCE, {
error: ResourceUnavailableError,
});
rpcErrorsMap.set(ERR_RPC_TRANSACTION_REJECTED, {
error: TransactionRejectedError,
});
rpcErrorsMap.set(ERR_RPC_MISSING_RESOURCE, {
error: ResourcesNotFoundError,
});
rpcErrorsMap.set(ERR_RPC_NOT_SUPPORTED, {
error: VersionNotSupportedError,
});
rpcErrorsMap.set(ERR_RPC_LIMIT_EXCEEDED, { error: LimitExceededError });