-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
response_errors.ts
104 lines (90 loc) · 3.38 KB
/
response_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
/*
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-next-line max-classes-per-file
import {
JsonRpcError,
JsonRpcPayload,
JsonRpcResponse,
JsonRpcResponseWithError,
} from 'web3-types';
import { BaseWeb3Error } from '../web3_error_base.js';
import { ERR_INVALID_RESPONSE, ERR_RESPONSE } from '../error_codes.js';
// To avoid circular package dependency, copied to code here. If you update this please update same function in `json_rpc.ts`
const isResponseWithError = <Error = unknown, Result = unknown>(
response: JsonRpcResponse<Result, Error>,
): response is JsonRpcResponseWithError<Error> =>
!Array.isArray(response) &&
response.jsonrpc === '2.0' &&
!!response &&
// eslint-disable-next-line no-null/no-null
(response.result === undefined || response.result === null) &&
// JSON RPC consider "null" as valid response
'error' in response &&
(typeof response.id === 'number' || typeof response.id === 'string');
const buildErrorMessage = (response: JsonRpcResponse<unknown, unknown>): string =>
isResponseWithError(response) ? response.error.message : '';
export class ResponseError<ErrorType = unknown, RequestType = unknown> extends BaseWeb3Error {
public code = ERR_RESPONSE;
public data?: ErrorType | ErrorType[];
public request?: JsonRpcPayload<RequestType>;
public constructor(
response: JsonRpcResponse<unknown, ErrorType>,
message?: string,
request?: JsonRpcPayload<RequestType>,
) {
super(
message ??
`Returned error: ${
Array.isArray(response)
? response.map(r => buildErrorMessage(r)).join(',')
: buildErrorMessage(response)
}`,
);
if (!message) {
this.data = Array.isArray(response)
? response.map(r => r.error?.data as ErrorType)
: response?.error?.data;
}
this.request = request;
let errorOrErrors: JsonRpcError | JsonRpcError[] | undefined;
if (`error` in response) {
errorOrErrors = response.error as JsonRpcError;
} else if (response instanceof Array) {
errorOrErrors = response.map(r => r.error) as JsonRpcError[];
}
this.innerError = errorOrErrors as Error | Error[] | undefined;
}
public toJSON() {
return { ...super.toJSON(), data: this.data, request: this.request };
}
}
export class InvalidResponseError<ErrorType = unknown, RequestType = unknown> extends ResponseError<
ErrorType,
RequestType
> {
public constructor(
result: JsonRpcResponse<unknown, ErrorType>,
request?: JsonRpcPayload<RequestType>,
) {
super(result, undefined, request);
this.code = ERR_INVALID_RESPONSE;
let errorOrErrors: JsonRpcError | JsonRpcError[] | undefined;
if (`error` in result) {
errorOrErrors = result.error as JsonRpcError;
} else if (result instanceof Array) {
errorOrErrors = result.map(r => r.error) as JsonRpcError[];
}
this.innerError = errorOrErrors as Error | Error[] | undefined;
}
}