eth-json-rpc-errors
Errors for the Ethereum JSON RPC and Ethereum Provider, and making unknown errors compliant with either spec.
Basic Usage
throw ethErrorsprovider// orthrow ethErrorsprovider
Supported Errors
- Ethereum JSON RPC
- Per EIP 1474
- This includes all JSON RPC 2.0 errors
- Per EIP 1474
- Ethereum Provider errors
- Per EIP 1193
- Does not yet support
CloseEvent
errors or status codes.
- Does not yet support
- Per EIP 1193
Usage
Installation: npm install eth-json-rpc-errors
or yarn add eth-json-rpc-errors
Import using ES6 syntax (no default export) or Node require
.
Errors API
// Ethereum RPC errors are namespaced under "ethErrors.rpc"responseerror = ethErrorsrpc // ETH JSON RPC errors namespaced under ethErrors.providerresponseerror = ethErrorsprovider // each error getter takes a single "opts" argument// for most errors, this can be replaced with a single string, which becomes// the error messageresponseerror = ethErrorsprovider // if an error getter accepts a single string, all arguments can be omittedresponseerror = ethErrorsproviderresponseerror = ethErrorsprovider // omitting the message will produce an error with a default message per// the relevant spec // omitting the data argument will produce an error without a// "data" property // the JSON RPC 2.0 server error requires a valid coderesponseerror = ethErrorsrpc // custom Ethereum Provider errors require a valid code and message// valid codes are integers i such that: 1000 <= i <= 4999responseerror = ethErrorsprovider
Parsing Unknown Errors
// this is useful for ensuring your errors are standardized // if the argument is not a valid error per any supported spec,// it will be added as error.data.originalErrorresponseerror = // you can add a custom fallback error code and message if desiredconst fallbackError = code: 4999 message: 'My custom error.' responseerror = // Note: if the original error has a "message" property, it will take// precedence over the fallback error's message // the default fallback is: code: -32603 message: 'Internal JSON-RPC error.'
Other Exports
/** * TypeScript interfaces */ /** * Classes */ /** * getMessageFromCode & ERROR_CODES */ // get the default message string for the given code, or a fallback message if// no message exists for the given codeconst message1 = // you can specify your own fallback messageconst message2 = // it can be anything, use at your own perilconst message3 = // {// jsonRpc: { [errorName]: code, ... },// eth: { [errorName]: code, ... },// }const code1 = ERROR_CODESrpcparseconst code2 = ERROR_CODESprovideruserRejectedRequest // all codes in ERROR_CODES have default messagesconst message4 = const message5 =
License
MIT