-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
contract_errors.ts
237 lines (197 loc) · 7.01 KB
/
contract_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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
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 { JsonRpcError, TransactionReceipt, HexString } from 'web3-types';
import {
ERR_CONTRACT,
ERR_CONTRACT_ABI_MISSING,
ERR_CONTRACT_EXECUTION_REVERTED,
ERR_CONTRACT_EVENT_NOT_EXISTS,
ERR_CONTRACT_INSTANTIATION,
ERR_CONTRACT_MISSING_ADDRESS,
ERR_CONTRACT_MISSING_DEPLOY_DATA,
ERR_CONTRACT_MISSING_FROM_ADDRESS,
ERR_CONTRACT_REQUIRED_CALLBACK,
ERR_CONTRACT_RESERVED_EVENT,
ERR_CONTRACT_RESOLVER_MISSING,
ERR_CONTRACT_TX_DATA_AND_INPUT,
} from '../error_codes.js';
import { BaseWeb3Error, InvalidValueError } from '../web3_error_base.js';
export class Web3ContractError extends BaseWeb3Error {
public code = ERR_CONTRACT;
public receipt?: TransactionReceipt;
public constructor(message: string, receipt?: TransactionReceipt) {
super(message);
this.receipt = receipt;
}
}
export class ResolverMethodMissingError extends BaseWeb3Error {
public code = ERR_CONTRACT_RESOLVER_MISSING;
public constructor(public address: string, public name: string) {
super(`The resolver at ${address} does not implement requested method: "${name}".`);
}
public toJSON() {
return { ...super.toJSON(), address: this.address, name: this.name };
}
}
export class ContractMissingABIError extends BaseWeb3Error {
public code = ERR_CONTRACT_ABI_MISSING;
public constructor() {
super(
'You must provide the json interface of the contract when instantiating a contract object.',
);
}
}
export class ContractOnceRequiresCallbackError extends BaseWeb3Error {
public code = ERR_CONTRACT_REQUIRED_CALLBACK;
public constructor() {
super('Once requires a callback as the second parameter.');
}
}
export class ContractEventDoesNotExistError extends BaseWeb3Error {
public code = ERR_CONTRACT_EVENT_NOT_EXISTS;
public constructor(public eventName: string) {
super(`Event "${eventName}" doesn't exist in this contract.`);
}
public toJSON() {
return { ...super.toJSON(), eventName: this.eventName };
}
}
export class ContractReservedEventError extends BaseWeb3Error {
public code = ERR_CONTRACT_RESERVED_EVENT;
public constructor(public type: string) {
super(`Event "${type}" doesn't exist in this contract.`);
}
public toJSON() {
return { ...super.toJSON(), type: this.type };
}
}
export class ContractMissingDeployDataError extends BaseWeb3Error {
public code = ERR_CONTRACT_MISSING_DEPLOY_DATA;
public constructor() {
super(`No "data" specified in neither the given options, nor the default options.`);
}
}
export class ContractNoAddressDefinedError extends BaseWeb3Error {
public code = ERR_CONTRACT_MISSING_ADDRESS;
public constructor() {
super("This contract object doesn't have address set yet, please set an address first.");
}
}
export class ContractNoFromAddressDefinedError extends BaseWeb3Error {
public code = ERR_CONTRACT_MISSING_FROM_ADDRESS;
public constructor() {
super('No "from" address specified in neither the given options, nor the default options.');
}
}
export class ContractInstantiationError extends BaseWeb3Error {
public code = ERR_CONTRACT_INSTANTIATION;
}
export type ProviderErrorData =
| HexString
| { data: HexString }
| { originalError: { data: HexString } };
/**
* This class is expected to be set as an `innerError` inside ContractExecutionError
* The properties would be typically decoded from the `data` if it was encoded according to EIP-838
*/
export class Eip838ExecutionError extends Web3ContractError {
public readonly name: string;
public code: number;
public data?: HexString;
public errorName?: string;
public errorSignature?: string;
public errorArgs?: { [K in string]: unknown };
// eslint-disable-next-line no-use-before-define
public innerError: Eip838ExecutionError | undefined;
public constructor(error: JsonRpcError<ProviderErrorData> | Eip838ExecutionError) {
super(error.message || 'Error');
this.name = ('name' in error && error.name) || this.constructor.name;
this.stack = ('stack' in error && error.stack) || undefined;
this.code = error.code;
// get embedded error details got from some providers like MetaMask
// and set this.data from the inner error data for easier read.
// note: the data is a hex string inside either:
// error.data, error.data.data or error.data.originalError.data (https://github.com/web3/web3.js/issues/4454#issuecomment-1485953455)
if (typeof error.data === 'object') {
let originalError: { data: string };
if ('originalError' in error.data) {
originalError = error.data.originalError;
} else {
// Ganache has no `originalError` sub-object unlike others
originalError = error.data;
}
this.data = originalError.data;
this.innerError = new Eip838ExecutionError(
originalError as JsonRpcError<ProviderErrorData>,
);
} else {
this.data = error.data;
}
}
public setDecodedProperties(
errorName: string,
errorSignature?: string,
errorArgs?: { [K in string]: unknown },
) {
this.errorName = errorName;
this.errorSignature = errorSignature;
this.errorArgs = errorArgs;
}
public toJSON() {
let json = {
...super.toJSON(),
data: this.data,
} as {
name: string;
code: number;
message: string;
innerError: Error | Error[] | undefined;
data: string;
errorName?: string;
errorSignature?: string;
errorArgs?: { [K in string]: unknown };
};
if (this.errorName) {
json = {
...json,
errorName: this.errorName,
errorSignature: this.errorSignature,
errorArgs: this.errorArgs,
};
}
return json;
}
}
/**
* Used when an error is raised while executing a function inside a smart contract.
* The data is expected to be encoded according to EIP-848.
*/
export class ContractExecutionError extends Web3ContractError {
public innerError: Eip838ExecutionError;
public constructor(rpcError: JsonRpcError) {
super('Error happened while trying to execute a function inside a smart contract');
this.code = ERR_CONTRACT_EXECUTION_REVERTED;
this.innerError = new Eip838ExecutionError(rpcError as JsonRpcError<ProviderErrorData>);
}
}
export class ContractTransactionDataAndInputError extends InvalidValueError {
public code = ERR_CONTRACT_TX_DATA_AND_INPUT;
public constructor(value: { data: HexString | undefined; input: HexString | undefined }) {
super(
`data: ${value.data ?? 'undefined'}, input: ${value.input ?? 'undefined'}`,
'You can\'t have "data" and "input" as properties of a contract at the same time, please use either "data" or "input" instead.',
);
}
}