errors.d.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /**
  2. * All errors in ethers include properties to ensure they are both
  3. * human-readable (i.e. ``.message``) and machine-readable (i.e. ``.code``).
  4. *
  5. * The [[isError]] function can be used to check the error ``code`` and
  6. * provide a type guard for the properties present on that error interface.
  7. *
  8. * @_section: api/utils/errors:Errors [about-errors]
  9. */
  10. import type { TransactionRequest, TransactionReceipt, TransactionResponse } from "../providers/index.js";
  11. import type { FetchRequest, FetchResponse } from "./fetch.js";
  12. /**
  13. * An error may contain additional properties, but those must not
  14. * conflict with any implicit properties.
  15. */
  16. export type ErrorInfo<T> = Omit<T, "code" | "name" | "message" | "shortMessage"> & {
  17. shortMessage?: string;
  18. };
  19. /**
  20. * All errors emitted by ethers have an **ErrorCode** to help
  21. * identify and coalesce errors to simplify programmatic analysis.
  22. *
  23. * Each **ErrorCode** is the %%code%% proerty of a coresponding
  24. * [[EthersError]].
  25. *
  26. * **Generic Errors**
  27. *
  28. * **``"UNKNOWN_ERROR"``** - see [[UnknownError]]
  29. *
  30. * **``"NOT_IMPLEMENTED"``** - see [[NotImplementedError]]
  31. *
  32. * **``"UNSUPPORTED_OPERATION"``** - see [[UnsupportedOperationError]]
  33. *
  34. * **``"NETWORK_ERROR"``** - see [[NetworkError]]
  35. *
  36. * **``"SERVER_ERROR"``** - see [[ServerError]]
  37. *
  38. * **``"TIMEOUT"``** - see [[TimeoutError]]
  39. *
  40. * **``"BAD_DATA"``** - see [[BadDataError]]
  41. *
  42. * **``"CANCELLED"``** - see [[CancelledError]]
  43. *
  44. * **Operational Errors**
  45. *
  46. * **``"BUFFER_OVERRUN"``** - see [[BufferOverrunError]]
  47. *
  48. * **``"NUMERIC_FAULT"``** - see [[NumericFaultError]]
  49. *
  50. * **Argument Errors**
  51. *
  52. * **``"INVALID_ARGUMENT"``** - see [[InvalidArgumentError]]
  53. *
  54. * **``"MISSING_ARGUMENT"``** - see [[MissingArgumentError]]
  55. *
  56. * **``"UNEXPECTED_ARGUMENT"``** - see [[UnexpectedArgumentError]]
  57. *
  58. * **``"VALUE_MISMATCH"``** - //unused//
  59. *
  60. * **Blockchain Errors**
  61. *
  62. * **``"CALL_EXCEPTION"``** - see [[CallExceptionError]]
  63. *
  64. * **``"INSUFFICIENT_FUNDS"``** - see [[InsufficientFundsError]]
  65. *
  66. * **``"NONCE_EXPIRED"``** - see [[NonceExpiredError]]
  67. *
  68. * **``"REPLACEMENT_UNDERPRICED"``** - see [[ReplacementUnderpricedError]]
  69. *
  70. * **``"TRANSACTION_REPLACED"``** - see [[TransactionReplacedError]]
  71. *
  72. * **``"UNCONFIGURED_NAME"``** - see [[UnconfiguredNameError]]
  73. *
  74. * **``"OFFCHAIN_FAULT"``** - see [[OffchainFaultError]]
  75. *
  76. * **User Interaction Errors**
  77. *
  78. * **``"ACTION_REJECTED"``** - see [[ActionRejectedError]]
  79. */
  80. export type ErrorCode = "UNKNOWN_ERROR" | "NOT_IMPLEMENTED" | "UNSUPPORTED_OPERATION" | "NETWORK_ERROR" | "SERVER_ERROR" | "TIMEOUT" | "BAD_DATA" | "CANCELLED" | "BUFFER_OVERRUN" | "NUMERIC_FAULT" | "INVALID_ARGUMENT" | "MISSING_ARGUMENT" | "UNEXPECTED_ARGUMENT" | "VALUE_MISMATCH" | "CALL_EXCEPTION" | "INSUFFICIENT_FUNDS" | "NONCE_EXPIRED" | "REPLACEMENT_UNDERPRICED" | "TRANSACTION_REPLACED" | "UNCONFIGURED_NAME" | "OFFCHAIN_FAULT" | "ACTION_REJECTED";
  81. /**
  82. * All errors in Ethers include properties to assist in
  83. * machine-readable errors.
  84. */
  85. export interface EthersError<T extends ErrorCode = ErrorCode> extends Error {
  86. /**
  87. * The string error code.
  88. */
  89. code: ErrorCode;
  90. /**
  91. * A short message describing the error, with minimal additional
  92. * details.
  93. */
  94. shortMessage: string;
  95. /**
  96. * Additional info regarding the error that may be useful.
  97. *
  98. * This is generally helpful mostly for human-based debugging.
  99. */
  100. info?: Record<string, any>;
  101. /**
  102. * Any related error.
  103. */
  104. error?: Error;
  105. }
  106. /**
  107. * This Error is a catch-all for when there is no way for Ethers to
  108. * know what the underlying problem is.
  109. */
  110. export interface UnknownError extends EthersError<"UNKNOWN_ERROR"> {
  111. [key: string]: any;
  112. }
  113. /**
  114. * This Error is mostly used as a stub for functionality that is
  115. * intended for the future, but is currently not implemented.
  116. */
  117. export interface NotImplementedError extends EthersError<"NOT_IMPLEMENTED"> {
  118. /**
  119. * The attempted operation.
  120. */
  121. operation: string;
  122. }
  123. /**
  124. * This Error indicates that the attempted operation is not supported.
  125. *
  126. * This could range from a specific JSON-RPC end-point not supporting
  127. * a feature to a specific configuration of an object prohibiting the
  128. * operation.
  129. *
  130. * For example, a [[Wallet]] with no connected [[Provider]] is unable
  131. * to send a transaction.
  132. */
  133. export interface UnsupportedOperationError extends EthersError<"UNSUPPORTED_OPERATION"> {
  134. /**
  135. * The attempted operation.
  136. */
  137. operation: string;
  138. }
  139. /**
  140. * This Error indicates a problem connecting to a network.
  141. */
  142. export interface NetworkError extends EthersError<"NETWORK_ERROR"> {
  143. /**
  144. * The network event.
  145. */
  146. event: string;
  147. }
  148. /**
  149. * This Error indicates there was a problem fetching a resource from
  150. * a server.
  151. */
  152. export interface ServerError extends EthersError<"SERVER_ERROR"> {
  153. /**
  154. * The requested resource.
  155. */
  156. request: FetchRequest | string;
  157. /**
  158. * The response received from the server, if available.
  159. */
  160. response?: FetchResponse;
  161. }
  162. /**
  163. * This Error indicates that the timeout duration has expired and
  164. * that the operation has been implicitly cancelled.
  165. *
  166. * The side-effect of the operation may still occur, as this
  167. * generally means a request has been sent and there has simply
  168. * been no response to indicate whether it was processed or not.
  169. */
  170. export interface TimeoutError extends EthersError<"TIMEOUT"> {
  171. /**
  172. * The attempted operation.
  173. */
  174. operation: string;
  175. /**
  176. * The reason.
  177. */
  178. reason: string;
  179. /**
  180. * The resource request, if available.
  181. */
  182. request?: FetchRequest;
  183. }
  184. /**
  185. * This Error indicates that a provided set of data cannot
  186. * be correctly interpreted.
  187. */
  188. export interface BadDataError extends EthersError<"BAD_DATA"> {
  189. /**
  190. * The data.
  191. */
  192. value: any;
  193. }
  194. /**
  195. * This Error indicates that the operation was cancelled by a
  196. * programmatic call, for example to ``cancel()``.
  197. */
  198. export interface CancelledError extends EthersError<"CANCELLED"> {
  199. }
  200. /**
  201. * This Error indicates an attempt was made to read outside the bounds
  202. * of protected data.
  203. *
  204. * Most operations in Ethers are protected by bounds checks, to mitigate
  205. * exploits when parsing data.
  206. */
  207. export interface BufferOverrunError extends EthersError<"BUFFER_OVERRUN"> {
  208. /**
  209. * The buffer that was overrun.
  210. */
  211. buffer: Uint8Array;
  212. /**
  213. * The length of the buffer.
  214. */
  215. length: number;
  216. /**
  217. * The offset that was requested.
  218. */
  219. offset: number;
  220. }
  221. /**
  222. * This Error indicates an operation which would result in incorrect
  223. * arithmetic output has occurred.
  224. *
  225. * For example, trying to divide by zero or using a ``uint8`` to store
  226. * a negative value.
  227. */
  228. export interface NumericFaultError extends EthersError<"NUMERIC_FAULT"> {
  229. /**
  230. * The attempted operation.
  231. */
  232. operation: string;
  233. /**
  234. * The fault reported.
  235. */
  236. fault: string;
  237. /**
  238. * The value the operation was attempted against.
  239. */
  240. value: any;
  241. }
  242. /**
  243. * This Error indicates an incorrect type or value was passed to
  244. * a function or method.
  245. */
  246. export interface InvalidArgumentError extends EthersError<"INVALID_ARGUMENT"> {
  247. /**
  248. * The name of the argument.
  249. */
  250. argument: string;
  251. /**
  252. * The value that was provided.
  253. */
  254. value: any;
  255. info?: Record<string, any>;
  256. }
  257. /**
  258. * This Error indicates there were too few arguments were provided.
  259. */
  260. export interface MissingArgumentError extends EthersError<"MISSING_ARGUMENT"> {
  261. /**
  262. * The number of arguments received.
  263. */
  264. count: number;
  265. /**
  266. * The number of arguments expected.
  267. */
  268. expectedCount: number;
  269. }
  270. /**
  271. * This Error indicates too many arguments were provided.
  272. */
  273. export interface UnexpectedArgumentError extends EthersError<"UNEXPECTED_ARGUMENT"> {
  274. /**
  275. * The number of arguments received.
  276. */
  277. count: number;
  278. /**
  279. * The number of arguments expected.
  280. */
  281. expectedCount: number;
  282. }
  283. /**
  284. * The action that resulted in the call exception.
  285. */
  286. export type CallExceptionAction = "call" | "estimateGas" | "getTransactionResult" | "sendTransaction" | "unknown";
  287. /**
  288. * The related transaction that caused the error.
  289. */
  290. export type CallExceptionTransaction = {
  291. to: null | string;
  292. from?: string;
  293. data: string;
  294. };
  295. /**
  296. * This **Error** indicates a transaction reverted.
  297. */
  298. export interface CallExceptionError extends EthersError<"CALL_EXCEPTION"> {
  299. /**
  300. * The action being performed when the revert was encountered.
  301. */
  302. action: CallExceptionAction;
  303. /**
  304. * The revert data returned.
  305. */
  306. data: null | string;
  307. /**
  308. * A human-readable representation of data, if possible.
  309. */
  310. reason: null | string;
  311. /**
  312. * The transaction that triggered the exception.
  313. */
  314. transaction: CallExceptionTransaction;
  315. /**
  316. * The contract invocation details, if available.
  317. */
  318. invocation: null | {
  319. method: string;
  320. signature: string;
  321. args: Array<any>;
  322. };
  323. /**
  324. * The built-in or custom revert error, if available
  325. */
  326. revert: null | {
  327. signature: string;
  328. name: string;
  329. args: Array<any>;
  330. };
  331. /**
  332. * If the error occurred in a transaction that was mined
  333. * (with a status of ``0``), this is the receipt.
  334. */
  335. receipt?: TransactionReceipt;
  336. }
  337. /**
  338. * The sending account has insufficient funds to cover the
  339. * entire transaction cost.
  340. */
  341. export interface InsufficientFundsError extends EthersError<"INSUFFICIENT_FUNDS"> {
  342. /**
  343. * The transaction.
  344. */
  345. transaction: TransactionRequest;
  346. }
  347. /**
  348. * The sending account has already used this nonce in a
  349. * transaction that has been included.
  350. */
  351. export interface NonceExpiredError extends EthersError<"NONCE_EXPIRED"> {
  352. /**
  353. * The transaction.
  354. */
  355. transaction: TransactionRequest;
  356. }
  357. /**
  358. * A CCIP-read exception, which cannot be recovered from or
  359. * be further processed.
  360. */
  361. export interface OffchainFaultError extends EthersError<"OFFCHAIN_FAULT"> {
  362. /**
  363. * The transaction.
  364. */
  365. transaction?: TransactionRequest;
  366. /**
  367. * The reason the CCIP-read failed.
  368. */
  369. reason: string;
  370. }
  371. /**
  372. * An attempt was made to replace a transaction, but with an
  373. * insufficient additional fee to afford evicting the old
  374. * transaction from the memory pool.
  375. */
  376. export interface ReplacementUnderpricedError extends EthersError<"REPLACEMENT_UNDERPRICED"> {
  377. /**
  378. * The transaction.
  379. */
  380. transaction: TransactionRequest;
  381. }
  382. /**
  383. * A pending transaction was replaced by another.
  384. */
  385. export interface TransactionReplacedError extends EthersError<"TRANSACTION_REPLACED"> {
  386. /**
  387. * If the transaction was cancelled, such that the original
  388. * effects of the transaction cannot be assured.
  389. */
  390. cancelled: boolean;
  391. /**
  392. * The reason the transaction was replaced.
  393. */
  394. reason: "repriced" | "cancelled" | "replaced";
  395. /**
  396. * The hash of the replaced transaction.
  397. */
  398. hash: string;
  399. /**
  400. * The transaction that replaced the transaction.
  401. */
  402. replacement: TransactionResponse;
  403. /**
  404. * The receipt of the transaction that replace the transaction.
  405. */
  406. receipt: TransactionReceipt;
  407. }
  408. /**
  409. * This Error indicates an ENS name was used, but the name has not
  410. * been configured.
  411. *
  412. * This could indicate an ENS name is unowned or that the current
  413. * address being pointed to is the [[ZeroAddress]].
  414. */
  415. export interface UnconfiguredNameError extends EthersError<"UNCONFIGURED_NAME"> {
  416. /**
  417. * The ENS name that was requested
  418. */
  419. value: string;
  420. }
  421. /**
  422. * This Error indicates a request was rejected by the user.
  423. *
  424. * In most clients (such as MetaMask), when an operation requires user
  425. * authorization (such as ``signer.sendTransaction``), the client
  426. * presents a dialog box to the user. If the user denies the request
  427. * this error is thrown.
  428. */
  429. export interface ActionRejectedError extends EthersError<"ACTION_REJECTED"> {
  430. /**
  431. * The requested action.
  432. */
  433. action: "requestAccess" | "sendTransaction" | "signMessage" | "signTransaction" | "signTypedData" | "unknown";
  434. /**
  435. * The reason the action was rejected.
  436. *
  437. * If there is already a pending request, some clients may indicate
  438. * there is already a ``"pending"`` action. This prevents an app
  439. * from spamming the user.
  440. */
  441. reason: "expired" | "rejected" | "pending";
  442. }
  443. /**
  444. * A conditional type that transforms the [[ErrorCode]] T into
  445. * its EthersError type.
  446. *
  447. * @flatworm-skip-docs
  448. */
  449. export type CodedEthersError<T> = T extends "UNKNOWN_ERROR" ? UnknownError : T extends "NOT_IMPLEMENTED" ? NotImplementedError : T extends "UNSUPPORTED_OPERATION" ? UnsupportedOperationError : T extends "NETWORK_ERROR" ? NetworkError : T extends "SERVER_ERROR" ? ServerError : T extends "TIMEOUT" ? TimeoutError : T extends "BAD_DATA" ? BadDataError : T extends "CANCELLED" ? CancelledError : T extends "BUFFER_OVERRUN" ? BufferOverrunError : T extends "NUMERIC_FAULT" ? NumericFaultError : T extends "INVALID_ARGUMENT" ? InvalidArgumentError : T extends "MISSING_ARGUMENT" ? MissingArgumentError : T extends "UNEXPECTED_ARGUMENT" ? UnexpectedArgumentError : T extends "CALL_EXCEPTION" ? CallExceptionError : T extends "INSUFFICIENT_FUNDS" ? InsufficientFundsError : T extends "NONCE_EXPIRED" ? NonceExpiredError : T extends "OFFCHAIN_FAULT" ? OffchainFaultError : T extends "REPLACEMENT_UNDERPRICED" ? ReplacementUnderpricedError : T extends "TRANSACTION_REPLACED" ? TransactionReplacedError : T extends "UNCONFIGURED_NAME" ? UnconfiguredNameError : T extends "ACTION_REJECTED" ? ActionRejectedError : never;
  450. /**
  451. * Returns true if the %%error%% matches an error thrown by ethers
  452. * that matches the error %%code%%.
  453. *
  454. * In TypeScript environments, this can be used to check that %%error%%
  455. * matches an EthersError type, which means the expected properties will
  456. * be set.
  457. *
  458. * @See [ErrorCodes](api:ErrorCode)
  459. * @example
  460. * try {
  461. * // code....
  462. * } catch (e) {
  463. * if (isError(e, "CALL_EXCEPTION")) {
  464. * // The Type Guard has validated this object
  465. * console.log(e.data);
  466. * }
  467. * }
  468. */
  469. export declare function isError<K extends ErrorCode, T extends CodedEthersError<K>>(error: any, code: K): error is T;
  470. /**
  471. * Returns true if %%error%% is a [[CallExceptionError].
  472. */
  473. export declare function isCallException(error: any): error is CallExceptionError;
  474. /**
  475. * Returns a new Error configured to the format ethers emits errors, with
  476. * the %%message%%, [[api:ErrorCode]] %%code%% and additional properties
  477. * for the corresponding EthersError.
  478. *
  479. * Each error in ethers includes the version of ethers, a
  480. * machine-readable [[ErrorCode]], and depending on %%code%%, additional
  481. * required properties. The error message will also include the %%message%%,
  482. * ethers version, %%code%% and all additional properties, serialized.
  483. */
  484. export declare function makeError<K extends ErrorCode, T extends CodedEthersError<K>>(message: string, code: K, info?: ErrorInfo<T>): T;
  485. /**
  486. * Throws an EthersError with %%message%%, %%code%% and additional error
  487. * %%info%% when %%check%% is falsish..
  488. *
  489. * @see [[api:makeError]]
  490. */
  491. export declare function assert<K extends ErrorCode, T extends CodedEthersError<K>>(check: unknown, message: string, code: K, info?: ErrorInfo<T>): asserts check;
  492. /**
  493. * A simple helper to simply ensuring provided arguments match expected
  494. * constraints, throwing if not.
  495. *
  496. * In TypeScript environments, the %%check%% has been asserted true, so
  497. * any further code does not need additional compile-time checks.
  498. */
  499. export declare function assertArgument(check: unknown, message: string, name: string, value: unknown): asserts check;
  500. export declare function assertArgumentCount(count: number, expectedCount: number, message?: string): void;
  501. /**
  502. * Throws if the normalization %%form%% is not supported.
  503. */
  504. export declare function assertNormalize(form: string): void;
  505. /**
  506. * Many classes use file-scoped values to guard the constructor,
  507. * making it effectively private. This facilitates that pattern
  508. * by ensuring the %%givenGaurd%% matches the file-scoped %%guard%%,
  509. * throwing if not, indicating the %%className%% if provided.
  510. */
  511. export declare function assertPrivate(givenGuard: any, guard: any, className?: string): void;
  512. //# sourceMappingURL=errors.d.ts.map