Installation: npm install eth-rpc-errors or yarn add eth-rpc-errors
import or require as normal (no default export).
The package is implemented in TypeScript, and all exports are typed.
Errors API
import { ethErrors } from 'eth-rpc-errors'// Ethereum RPC errors are namespaced under "ethErrors.rpc"response.error = ethErrors.rpc.methodNotFound({ message: optionalCustomMessage, data: optionalData})// Provider errors namespaced under ethErrors.providerresponse.error = ethErrors.provider.unauthorized({ message: optionalCustomMessage, data: optionalData})// each error getter takes a single "opts" argument// for most errors, this can be replaced with a single string, which becomes// the error messageresponse.error = ethErrors.provider.unauthorized(customMessage)// if an error getter accepts a single string, all arguments can be omittedresponse.error = ethErrors.provider.unauthorized()response.error = ethErrors.provider.unauthorized({})
Parsing Unknown Errors
// this is useful for ensuring your errors are standardizedimport { serializeError } from 'eth-rpc-errors'// if the argument is not a valid error per any supported spec,// it will be added as error.data.originalErrorresponse.error = serializeError(maybeAnError)// you can add a custom fallback error code and message if desiredconst fallbackError = { code: 4999, message: 'My custom error.' }response.error = serializeError(maybeAnError, fallbackError)// 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
/** * Classes */import { EthereumRpcError, EthereumProviderError } from 'eth-rpc-errors'/** * getMessageFromCode and errorCodes */import { getMessageFromCode, errorCodes } from 'eth-rpc-errors'// get the default message string for the given code, or a fallback message if// no message exists for the given codeconst message1 = getMessageFromCode(someCode)// you can specify your own fallback messageconst message2 = getMessageFromCode(someCode, myFallback)// it can be anything, use at your own perilconst message3 = getMessageFromCode(someCode, null)// {// rpc: { [errorName]: code, ... },// provider: { [errorName]: code, ... },// }const