-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
rpc_errors.ts
165 lines (145 loc) · 4.98 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
/*
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';
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 ?? `An Rpc error has occured with a code of ${rpcError.error.code}`);
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 ParseError extends RpcError {
public code = ERR_RPC_INVALID_JSON;
public constructor(rpcError: JsonRpcResponseWithError) {
super(rpcError, 'Parse error');
}
}
export class InvalidRequestError extends RpcError {
public code = ERR_RPC_INVALID_REQUEST;
public constructor(rpcError: JsonRpcResponseWithError) {
super(rpcError, 'Invalid request');
}
}
export class MethodNotFoundError extends RpcError {
public code = ERR_RPC_INVALID_METHOD;
public constructor(rpcError: JsonRpcResponseWithError) {
super(rpcError, 'Method not found');
}
}
export class InvalidParamsError extends RpcError {
public code = ERR_RPC_INVALID_PARAMS;
public constructor(rpcError: JsonRpcResponseWithError) {
super(rpcError, 'Invalid request');
}
}
export class InternalError extends RpcError {
public code = ERR_RPC_INTERNAL_ERROR;
public constructor(rpcError: JsonRpcResponseWithError) {
super(rpcError, 'Internal error');
}
}
export class InvalidInputError extends RpcError {
public code = ERR_RPC_INVALID_INPUT;
public constructor(rpcError: JsonRpcResponseWithError) {
super(rpcError, 'Invalid input');
}
}
export class MethodNotSupported extends RpcError {
public code = ERR_RPC_UNSUPPORTED_METHOD;
public constructor(rpcError: JsonRpcResponseWithError) {
super(rpcError, 'Method not supported');
}
}
export class ResourceUnavailableError extends RpcError {
public code = ERR_RPC_UNAVAILABLE_RESOURCE;
public constructor(rpcError: JsonRpcResponseWithError) {
super(rpcError, 'Resource unavailable');
}
}
export class ResourcesNotFoundError extends RpcError {
public code = ERR_RPC_MISSING_RESOURCE;
public constructor(rpcError: JsonRpcResponseWithError) {
super(rpcError, 'Resource not found');
}
}
export class VersionNotSupportedError extends RpcError {
public code = ERR_RPC_NOT_SUPPORTED;
public constructor(rpcError: JsonRpcResponseWithError) {
super(rpcError, 'JSON-RPC version not supported');
}
}
export class TransactionRejectedError extends RpcError {
public code = ERR_RPC_TRANSACTION_REJECTED;
public constructor(rpcError: JsonRpcResponseWithError) {
super(rpcError, 'Transaction rejected');
}
}
export class LimitExceededError extends RpcError {
public code = ERR_RPC_LIMIT_EXCEEDED;
public constructor(rpcError: JsonRpcResponseWithError) {
super(rpcError, 'Limit exceeded');
}
}
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 });