ethers.js/lib.esm/utils/errors.d.ts

512 lines
16 KiB
TypeScript
Raw Permalink Normal View History

2022-11-30 15:44:23 -05:00
/**
2023-06-01 17:52:58 -04:00
* All errors in ethers include properties to ensure they are both
* human-readable (i.e. ``.message``) and machine-readable (i.e. ``.code``).
*
* The [[isError]] function can be used to check the error ``code`` and
* provide a type guard for the properties present on that error interface.
2022-11-30 15:44:23 -05:00
*
2022-12-02 21:27:06 -05:00
* @_section: api/utils/errors:Errors [about-errors]
2022-11-30 15:44:23 -05:00
*/
2022-09-15 22:58:45 -04:00
import type { TransactionRequest, TransactionReceipt, TransactionResponse } from "../providers/index.js";
import type { FetchRequest, FetchResponse } from "./fetch.js";
2023-06-01 17:52:58 -04:00
/**
* An error may contain additional properties, but those must not
2023-11-11 18:00:39 -05:00
* conflict with any implicit properties.
2023-06-01 17:52:58 -04:00
*/
2023-10-09 20:27:56 -04:00
export type ErrorInfo<T> = Omit<T, "code" | "name" | "message" | "shortMessage"> & {
shortMessage?: string;
};
2022-09-15 22:58:45 -04:00
/**
* All errors emitted by ethers have an **ErrorCode** to help
2023-11-11 18:00:39 -05:00
* identify and coalesce errors to simplify programmatic analysis.
2022-09-15 22:58:45 -04:00
*
2023-02-04 03:26:34 -05:00
* Each **ErrorCode** is the %%code%% proerty of a coresponding
* [[EthersError]].
2022-09-15 22:58:45 -04:00
*
2023-02-04 03:26:34 -05:00
* **Generic Errors**
*
* **``"UNKNOWN_ERROR"``** - see [[UnknownError]]
*
* **``"NOT_IMPLEMENTED"``** - see [[NotImplementedError]]
*
* **``"UNSUPPORTED_OPERATION"``** - see [[UnsupportedOperationError]]
*
* **``"NETWORK_ERROR"``** - see [[NetworkError]]
*
* **``"SERVER_ERROR"``** - see [[ServerError]]
*
* **``"TIMEOUT"``** - see [[TimeoutError]]
*
* **``"BAD_DATA"``** - see [[BadDataError]]
*
* **``"CANCELLED"``** - see [[CancelledError]]
*
* **Operational Errors**
*
* **``"BUFFER_OVERRUN"``** - see [[BufferOverrunError]]
*
* **``"NUMERIC_FAULT"``** - see [[NumericFaultError]]
*
* **Argument Errors**
*
* **``"INVALID_ARGUMENT"``** - see [[InvalidArgumentError]]
*
* **``"MISSING_ARGUMENT"``** - see [[MissingArgumentError]]
*
* **``"UNEXPECTED_ARGUMENT"``** - see [[UnexpectedArgumentError]]
*
* **``"VALUE_MISMATCH"``** - //unused//
*
* **Blockchain Errors**
*
* **``"CALL_EXCEPTION"``** - see [[CallExceptionError]]
*
* **``"INSUFFICIENT_FUNDS"``** - see [[InsufficientFundsError]]
*
* **``"NONCE_EXPIRED"``** - see [[NonceExpiredError]]
*
* **``"REPLACEMENT_UNDERPRICED"``** - see [[ReplacementUnderpricedError]]
*
* **``"TRANSACTION_REPLACED"``** - see [[TransactionReplacedError]]
*
* **``"UNCONFIGURED_NAME"``** - see [[UnconfiguredNameError]]
*
* **``"OFFCHAIN_FAULT"``** - see [[OffchainFaultError]]
*
* **User Interaction Errors**
*
* **``"ACTION_REJECTED"``** - see [[ActionRejectedError]]
2022-09-15 22:58:45 -04:00
*/
2023-02-02 04:05:47 -05:00
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";
2023-01-28 01:53:29 -05:00
/**
* All errors in Ethers include properties to assist in
* machine-readable errors.
*/
2022-09-05 16:57:11 -04:00
export interface EthersError<T extends ErrorCode = ErrorCode> extends Error {
2023-01-28 01:53:29 -05:00
/**
* The string error code.
*/
2022-09-05 16:57:11 -04:00
code: ErrorCode;
2023-10-09 20:27:56 -04:00
/**
* A short message describing the error, with minimal additional
* details.
*/
shortMessage: string;
2023-01-28 01:53:29 -05:00
/**
* Additional info regarding the error that may be useful.
*
* This is generally helpful mostly for human-based debugging.
*/
2022-09-05 16:57:11 -04:00
info?: Record<string, any>;
2023-01-28 01:53:29 -05:00
/**
* Any related error.
*/
2022-09-05 16:57:11 -04:00
error?: Error;
}
2022-11-30 15:44:23 -05:00
/**
* This Error is a catch-all for when there is no way for Ethers to
* know what the underlying problem is.
*/
2022-09-05 16:57:11 -04:00
export interface UnknownError extends EthersError<"UNKNOWN_ERROR"> {
[key: string]: any;
}
2022-11-30 15:44:23 -05:00
/**
* This Error is mostly used as a stub for functionality that is
* intended for the future, but is currently not implemented.
*/
2022-09-05 16:57:11 -04:00
export interface NotImplementedError extends EthersError<"NOT_IMPLEMENTED"> {
2022-11-30 15:44:23 -05:00
/**
* The attempted operation.
*/
2022-09-05 16:57:11 -04:00
operation: string;
}
2022-11-30 15:44:23 -05:00
/**
* This Error indicates that the attempted operation is not supported.
*
2023-11-11 18:00:39 -05:00
* This could range from a specific JSON-RPC end-point not supporting
2022-11-30 15:44:23 -05:00
* a feature to a specific configuration of an object prohibiting the
* operation.
*
* For example, a [[Wallet]] with no connected [[Provider]] is unable
* to send a transaction.
*/
2022-09-05 16:57:11 -04:00
export interface UnsupportedOperationError extends EthersError<"UNSUPPORTED_OPERATION"> {
2022-11-30 15:44:23 -05:00
/**
* The attempted operation.
*/
2022-09-05 16:57:11 -04:00
operation: string;
}
2023-06-01 17:52:58 -04:00
/**
2023-11-11 18:00:39 -05:00
* This Error indicates a problem connecting to a network.
2023-06-01 17:52:58 -04:00
*/
2022-09-05 16:57:11 -04:00
export interface NetworkError extends EthersError<"NETWORK_ERROR"> {
2023-06-06 22:42:28 -04:00
/**
* The network event.
*/
2022-09-05 16:57:11 -04:00
event: string;
}
2022-11-30 15:44:23 -05:00
/**
* This Error indicates there was a problem fetching a resource from
* a server.
*/
2022-09-05 16:57:11 -04:00
export interface ServerError extends EthersError<"SERVER_ERROR"> {
2022-11-30 15:44:23 -05:00
/**
* The requested resource.
*/
2022-09-15 22:58:45 -04:00
request: FetchRequest | string;
2022-11-30 15:44:23 -05:00
/**
* The response received from the server, if available.
*/
2022-09-15 22:58:45 -04:00
response?: FetchResponse;
2022-09-05 16:57:11 -04:00
}
2022-11-30 15:44:23 -05:00
/**
* This Error indicates that the timeout duration has expired and
* that the operation has been implicitly cancelled.
*
* The side-effect of the operation may still occur, as this
* generally means a request has been sent and there has simply
* been no response to indicate whether it was processed or not.
*/
2022-09-05 16:57:11 -04:00
export interface TimeoutError extends EthersError<"TIMEOUT"> {
2022-11-30 15:44:23 -05:00
/**
* The attempted operation.
*/
2022-09-05 16:57:11 -04:00
operation: string;
2022-11-30 15:44:23 -05:00
/**
* The reason.
*/
2022-09-05 16:57:11 -04:00
reason: string;
2022-11-30 15:44:23 -05:00
/**
* The resource request, if available.
*/
2022-09-15 22:58:45 -04:00
request?: FetchRequest;
2022-09-05 16:57:11 -04:00
}
2022-11-30 15:44:23 -05:00
/**
* This Error indicates that a provided set of data cannot
2023-11-11 18:00:39 -05:00
* be correctly interpreted.
2022-11-30 15:44:23 -05:00
*/
2022-09-05 16:57:11 -04:00
export interface BadDataError extends EthersError<"BAD_DATA"> {
2022-11-30 15:44:23 -05:00
/**
* The data.
*/
2022-09-05 16:57:11 -04:00
value: any;
}
2022-11-30 15:44:23 -05:00
/**
* This Error indicates that the operation was cancelled by a
* programmatic call, for example to ``cancel()``.
*/
2022-09-05 16:57:11 -04:00
export interface CancelledError extends EthersError<"CANCELLED"> {
}
2022-11-30 15:44:23 -05:00
/**
* This Error indicates an attempt was made to read outside the bounds
* of protected data.
*
* Most operations in Ethers are protected by bounds checks, to mitigate
* exploits when parsing data.
*/
2022-09-05 16:57:11 -04:00
export interface BufferOverrunError extends EthersError<"BUFFER_OVERRUN"> {
2022-11-30 15:44:23 -05:00
/**
* The buffer that was overrun.
*/
2022-09-05 16:57:11 -04:00
buffer: Uint8Array;
2022-11-30 15:44:23 -05:00
/**
* The length of the buffer.
*/
2022-09-05 16:57:11 -04:00
length: number;
2022-11-30 15:44:23 -05:00
/**
* The offset that was requested.
*/
2022-09-05 16:57:11 -04:00
offset: number;
}
2022-11-30 15:44:23 -05:00
/**
* This Error indicates an operation which would result in incorrect
* arithmetic output has occurred.
*
* For example, trying to divide by zero or using a ``uint8`` to store
* a negative value.
*/
2022-09-05 16:57:11 -04:00
export interface NumericFaultError extends EthersError<"NUMERIC_FAULT"> {
2022-11-30 15:44:23 -05:00
/**
* The attempted operation.
*/
2022-09-05 16:57:11 -04:00
operation: string;
2022-11-30 15:44:23 -05:00
/**
* The fault reported.
*/
2022-09-05 16:57:11 -04:00
fault: string;
2022-11-30 15:44:23 -05:00
/**
* The value the operation was attempted against.
*/
2022-09-05 16:57:11 -04:00
value: any;
}
2022-11-30 15:44:23 -05:00
/**
* This Error indicates an incorrect type or value was passed to
* a function or method.
*/
2022-09-05 16:57:11 -04:00
export interface InvalidArgumentError extends EthersError<"INVALID_ARGUMENT"> {
2022-11-30 15:44:23 -05:00
/**
* The name of the argument.
*/
2022-09-05 16:57:11 -04:00
argument: string;
2022-11-30 15:44:23 -05:00
/**
* The value that was provided.
*/
2022-09-05 16:57:11 -04:00
value: any;
info?: Record<string, any>;
}
2023-01-28 01:53:29 -05:00
/**
* This Error indicates there were too few arguments were provided.
*/
2022-09-05 16:57:11 -04:00
export interface MissingArgumentError extends EthersError<"MISSING_ARGUMENT"> {
2023-01-28 01:53:29 -05:00
/**
* The number of arguments received.
*/
2022-09-05 16:57:11 -04:00
count: number;
2023-01-28 01:53:29 -05:00
/**
* The number of arguments expected.
*/
2022-09-05 16:57:11 -04:00
expectedCount: number;
}
2023-01-28 01:53:29 -05:00
/**
* This Error indicates too many arguments were provided.
*/
2022-09-05 16:57:11 -04:00
export interface UnexpectedArgumentError extends EthersError<"UNEXPECTED_ARGUMENT"> {
2023-01-28 01:53:29 -05:00
/**
* The number of arguments received.
*/
2022-09-05 16:57:11 -04:00
count: number;
2023-01-28 01:53:29 -05:00
/**
* The number of arguments expected.
*/
2022-09-05 16:57:11 -04:00
expectedCount: number;
}
2023-01-28 01:53:29 -05:00
/**
2023-08-02 23:42:17 -04:00
* The action that resulted in the call exception.
2023-01-28 01:53:29 -05:00
*/
2023-08-02 23:42:17 -04:00
export type CallExceptionAction = "call" | "estimateGas" | "getTransactionResult" | "sendTransaction" | "unknown";
2023-01-28 01:53:29 -05:00
/**
* The related transaction that caused the error.
*/
2023-02-02 04:05:47 -05:00
export type CallExceptionTransaction = {
2022-10-20 05:03:32 -04:00
to: null | string;
from?: string;
2022-09-05 16:57:11 -04:00
data: string;
2022-10-20 05:03:32 -04:00
};
2023-01-28 01:53:29 -05:00
/**
* This **Error** indicates a transaction reverted.
*/
2022-10-20 05:03:32 -04:00
export interface CallExceptionError extends EthersError<"CALL_EXCEPTION"> {
2023-01-28 01:53:29 -05:00
/**
* The action being performed when the revert was encountered.
*/
2022-10-20 05:03:32 -04:00
action: CallExceptionAction;
2023-01-28 01:53:29 -05:00
/**
* The revert data returned.
*/
2022-10-20 05:03:32 -04:00
data: null | string;
2023-01-28 01:53:29 -05:00
/**
* A human-readable representation of data, if possible.
*/
2022-10-20 05:03:32 -04:00
reason: null | string;
2023-01-28 01:53:29 -05:00
/**
* The transaction that triggered the exception.
*/
2022-10-20 05:03:32 -04:00
transaction: CallExceptionTransaction;
2023-01-28 01:53:29 -05:00
/**
* The contract invocation details, if available.
*/
2022-10-20 05:03:32 -04:00
invocation: null | {
method: string;
signature: string;
args: Array<any>;
};
2023-01-28 01:53:29 -05:00
/**
* The built-in or custom revert error, if available
*/
2022-10-20 05:03:32 -04:00
revert: null | {
signature: string;
name: string;
args: Array<any>;
};
2023-08-02 23:42:17 -04:00
/**
* If the error occurred in a transaction that was mined
* (with a status of ``0``), this is the receipt.
*/
receipt?: TransactionReceipt;
2022-09-05 16:57:11 -04:00
}
2023-01-28 01:53:29 -05:00
/**
* The sending account has insufficient funds to cover the
* entire transaction cost.
*/
2022-09-05 16:57:11 -04:00
export interface InsufficientFundsError extends EthersError<"INSUFFICIENT_FUNDS"> {
2023-01-28 01:53:29 -05:00
/**
* The transaction.
*/
2022-09-15 22:58:45 -04:00
transaction: TransactionRequest;
2022-09-05 16:57:11 -04:00
}
2023-01-28 01:53:29 -05:00
/**
* The sending account has already used this nonce in a
* transaction that has been included.
*/
2022-09-05 16:57:11 -04:00
export interface NonceExpiredError extends EthersError<"NONCE_EXPIRED"> {
2023-01-28 01:53:29 -05:00
/**
* The transaction.
*/
2022-09-15 22:58:45 -04:00
transaction: TransactionRequest;
2022-09-05 16:57:11 -04:00
}
2023-01-28 01:53:29 -05:00
/**
* A CCIP-read exception, which cannot be recovered from or
* be further processed.
*/
2022-09-05 16:57:11 -04:00
export interface OffchainFaultError extends EthersError<"OFFCHAIN_FAULT"> {
2023-01-28 01:53:29 -05:00
/**
* The transaction.
*/
2022-09-15 22:58:45 -04:00
transaction?: TransactionRequest;
2023-01-28 01:53:29 -05:00
/**
* The reason the CCIP-read failed.
*/
2022-09-05 16:57:11 -04:00
reason: string;
}
2023-01-28 01:53:29 -05:00
/**
* An attempt was made to replace a transaction, but with an
* insufficient additional fee to afford evicting the old
* transaction from the memory pool.
*/
2022-09-05 16:57:11 -04:00
export interface ReplacementUnderpricedError extends EthersError<"REPLACEMENT_UNDERPRICED"> {
2023-01-28 01:53:29 -05:00
/**
* The transaction.
*/
2022-09-15 22:58:45 -04:00
transaction: TransactionRequest;
2022-09-05 16:57:11 -04:00
}
2023-01-28 01:53:29 -05:00
/**
* A pending transaction was replaced by another.
*/
2022-09-05 16:57:11 -04:00
export interface TransactionReplacedError extends EthersError<"TRANSACTION_REPLACED"> {
2023-01-28 01:53:29 -05:00
/**
* If the transaction was cancelled, such that the original
* effects of the transaction cannot be assured.
*/
2022-09-05 16:57:11 -04:00
cancelled: boolean;
2023-01-28 01:53:29 -05:00
/**
* The reason the transaction was replaced.
*/
2022-09-05 16:57:11 -04:00
reason: "repriced" | "cancelled" | "replaced";
2023-01-28 01:53:29 -05:00
/**
* The hash of the replaced transaction.
*/
2022-09-05 16:57:11 -04:00
hash: string;
2023-01-28 01:53:29 -05:00
/**
* The transaction that replaced the transaction.
*/
2022-09-15 22:58:45 -04:00
replacement: TransactionResponse;
2023-01-28 01:53:29 -05:00
/**
* The receipt of the transaction that replace the transaction.
*/
2022-09-15 22:58:45 -04:00
receipt: TransactionReceipt;
2022-09-05 16:57:11 -04:00
}
2022-11-30 15:44:23 -05:00
/**
* This Error indicates an ENS name was used, but the name has not
* been configured.
*
* This could indicate an ENS name is unowned or that the current
2023-02-04 03:26:34 -05:00
* address being pointed to is the [[ZeroAddress]].
2022-11-30 15:44:23 -05:00
*/
2022-09-05 16:57:11 -04:00
export interface UnconfiguredNameError extends EthersError<"UNCONFIGURED_NAME"> {
2022-11-30 15:44:23 -05:00
/**
* The ENS name that was requested
*/
2022-09-05 16:57:11 -04:00
value: string;
}
2022-11-30 15:44:23 -05:00
/**
* This Error indicates a request was rejected by the user.
*
* In most clients (such as MetaMask), when an operation requires user
* authorization (such as ``signer.sendTransaction``), the client
* presents a dialog box to the user. If the user denies the request
* this error is thrown.
*/
2022-09-05 16:57:11 -04:00
export interface ActionRejectedError extends EthersError<"ACTION_REJECTED"> {
2022-11-30 15:44:23 -05:00
/**
* The requested action.
*/
2022-10-20 05:03:32 -04:00
action: "requestAccess" | "sendTransaction" | "signMessage" | "signTransaction" | "signTypedData" | "unknown";
2022-11-30 15:44:23 -05:00
/**
* The reason the action was rejected.
*
* If there is already a pending request, some clients may indicate
* there is already a ``"pending"`` action. This prevents an app
* from spamming the user.
*/
2022-10-20 05:03:32 -04:00
reason: "expired" | "rejected" | "pending";
2022-09-05 16:57:11 -04:00
}
2022-09-15 22:58:45 -04:00
/**
* A conditional type that transforms the [[ErrorCode]] T into
* its EthersError type.
*
* @flatworm-skip-docs
*/
2023-02-02 04:05:47 -05:00
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;
2022-09-05 16:57:11 -04:00
/**
2022-09-15 22:58:45 -04:00
* Returns true if the %%error%% matches an error thrown by ethers
* that matches the error %%code%%.
*
2023-11-11 18:00:39 -05:00
* In TypeScript environments, this can be used to check that %%error%%
2022-09-15 22:58:45 -04:00
* matches an EthersError type, which means the expected properties will
* be set.
*
* @See [ErrorCodes](api:ErrorCode)
* @example
2022-12-09 18:24:58 -05:00
* try {
* // code....
* } catch (e) {
2022-09-15 22:58:45 -04:00
* if (isError(e, "CALL_EXCEPTION")) {
2022-12-09 18:24:58 -05:00
* // The Type Guard has validated this object
2022-09-05 16:57:11 -04:00
* console.log(e.data);
* }
2022-12-09 18:24:58 -05:00
* }
2022-09-05 16:57:11 -04:00
*/
export declare function isError<K extends ErrorCode, T extends CodedEthersError<K>>(error: any, code: K): error is T;
2022-09-15 22:58:45 -04:00
/**
2023-02-04 03:26:34 -05:00
* Returns true if %%error%% is a [[CallExceptionError].
2022-09-15 22:58:45 -04:00
*/
2022-09-05 16:57:11 -04:00
export declare function isCallException(error: any): error is CallExceptionError;
2022-09-15 22:58:45 -04:00
/**
* Returns a new Error configured to the format ethers emits errors, with
2023-11-11 18:00:39 -05:00
* the %%message%%, [[api:ErrorCode]] %%code%% and additional properties
2022-09-15 22:58:45 -04:00
* for the corresponding EthersError.
*
* Each error in ethers includes the version of ethers, a
2023-11-11 18:00:39 -05:00
* machine-readable [[ErrorCode]], and depending on %%code%%, additional
* required properties. The error message will also include the %%message%%,
* ethers version, %%code%% and all additional properties, serialized.
2022-09-15 22:58:45 -04:00
*/
export declare function makeError<K extends ErrorCode, T extends CodedEthersError<K>>(message: string, code: K, info?: ErrorInfo<T>): T;
/**
* Throws an EthersError with %%message%%, %%code%% and additional error
2022-11-09 02:57:02 -05:00
* %%info%% when %%check%% is falsish..
2022-09-15 22:58:45 -04:00
*
* @see [[api:makeError]]
*/
2022-11-09 02:57:02 -05:00
export declare function assert<K extends ErrorCode, T extends CodedEthersError<K>>(check: unknown, message: string, code: K, info?: ErrorInfo<T>): asserts check;
2022-09-15 22:58:45 -04:00
/**
* A simple helper to simply ensuring provided arguments match expected
* constraints, throwing if not.
*
* In TypeScript environments, the %%check%% has been asserted true, so
* any further code does not need additional compile-time checks.
*/
export declare function assertArgument(check: unknown, message: string, name: string, value: unknown): asserts check;
export declare function assertArgumentCount(count: number, expectedCount: number, message?: string): void;
/**
* Throws if the normalization %%form%% is not supported.
*/
export declare function assertNormalize(form: string): void;
/**
* Many classes use file-scoped values to guard the constructor,
* making it effectively private. This facilitates that pattern
* by ensuring the %%givenGaurd%% matches the file-scoped %%guard%%,
* throwing if not, indicating the %%className%% if provided.
*/
export declare function assertPrivate(givenGuard: any, guard: any, className?: string): void;
2022-09-05 16:57:11 -04:00
//# sourceMappingURL=errors.d.ts.map