4.0.3 • Published 3 years ago

eth-rpc-errors v4.0.3

Weekly downloads
41,534
License
MIT
Repository
github
Last release
3 years ago

eth-rpc-errors

Ethereum RPC errors, including for Ethereum JSON RPC and Ethereum Provider, and making unknown errors compliant with either spec.

Basic Usage

In TypeScript or JavaScript:

import { ethErrors } from 'eth-rpc-errors'

throw ethErrors.provider.unauthorized()
// or
throw ethErrors.provider.unauthorized('my custom message')

Supported Errors

Usage

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.provider
response.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 message
response.error = ethErrors.provider.unauthorized(customMessage)

// if an error getter accepts a single string, all arguments can be omitted
response.error = ethErrors.provider.unauthorized()
response.error = ethErrors.provider.unauthorized({})

// 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 code
response.error = ethErrors.rpc.server({
  code: -32031
})

// custom Ethereum Provider errors require a valid code and message
// valid codes are integers i such that: 1000 <= i <= 4999
response.error = ethErrors.provider.custom({
  code: 1001, message: 'foo'
})

Parsing Unknown Errors

// this is useful for ensuring your errors are standardized
import { serializeError } from 'eth-rpc-errors'

// if the argument is not a valid error per any supported spec,
// it will be added as error.data.originalError
response.error = serializeError(maybeAnError)

// you can add a custom fallback error code and message if desired
const 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 code
const message1 = getMessageFromCode(someCode)

// you can specify your own fallback message
const message2 = getMessageFromCode(someCode, myFallback)
// it can be anything, use at your own peril
const message3 = getMessageFromCode(someCode, null)

// {
//   rpc: { [errorName]: code, ... },
//   provider: { [errorName]: code, ... },
// }
const code1 = errorCodes.rpc.parse
const code2 = errorCodes.provider.userRejectedRequest

// all codes in errorCodes have default messages
const message4 = getMessageFromCode(code1)
const message5 = getMessageFromCode(code2)

License

MIT

@muhammadwajidshahid/tronlink-provider@lilpacy/torus-embedaleo-snap@gelatonetwork/js-resolver-sdk@infinitebrahmanuniverse/nolb-eth-aleph-zero-snap@alex-tiplink/dangerous-embed@everything-registry/sub-chunk-1618facereiustofdao-controllersextrawallet-controllerforked-metamask-providersenefti-galerieth-json-rpc-backboneeth-json-rpc-middlewareeth-json-rpc-middleware-fetch-optionseth-json-rpc-infuraestabeumevenietexercitationemvoluptatemexpeditaminima@hashup-it/hashup-contracts@goldium/controllers@gelatonetwork/web3-functions-sdk@getsafle/safle-web3-engine@holdstation/controllersint-json-rpc-middleware@gasnow/inpage-providerinventorenoninventorequis@hulohot/controllersdropkit.jsdynamics-contracts-x@estebanmino/controllersgassefalsgassefals-2go23-provider-engine@edjowa/tronlink-provider@fksyuan/controllers@fksyuan/eth-json-rpc-middleware@fksyuan/rpc-cap@firefly-exchange/firefly-client@firefly-exchange/bluefin-client-sui@extrawallet/controllers@extrawallet/providers@fdao/controllers@iskraworld/iskra-sdk@iskraworld/wallet-provider@iskraworld/wallet-provider-basejson-rpc-engine@intercroneswap/tronlink-provider@imkey/web3-provider@ifanshx/cycgodsklip-web3-provider-mobile@jccdex/providers@klaytn/kaikas-web3-provider@klaytn/klip-web3-provider@keystonehq/controllers@keepkey/hdwallet-metamask@keep7777/onekey-cosmos-provider@keep7777/onekey-eth-provider@keep7777/onekey-aptos-provider@keep7777/onekey-polkadot-provider@keep7777/onekey-starcoin-provider@keep7777/onekey-sui-provider@skilm/providers@satwallet/wallet-messagenumquamofficia@rfoxlabs/rfox-kit@robincore/flutter-dapp-providermycel-widget@opentron/tronlink-provider@parallelnft/web3modalnavh-metamask-rpc-methodsnavh-metamask-snaps-controllersnavh-metamask-snaps-execution-environmentsnavh-metamask-snaps-utils@starcoin-org/stc-inpage-provider@starcoin-org/stc-json-rpc-middleware@starcoin/stc-inpage-provider@starcoin/stc-inpage-providers@starcoin/stc-json-rpc@starcoin/stc-json-rpc-middleware@rango-dev/signer-evm@prajjawal/qrl_providers@qubic-js/eth-json-rpc-infura@qubic-js/browser@qubic-js/core@rabby-wallet/page-provider@rabby-wallet/rabby-api@rarible/web3-ethereum@purefi/kyc-sdk@purefi/verifier-sdk@purefi/verifier-widget@refinableco/refinable-sdkomnismaxime@shapeshiftoss/hdwallet-shapeshift-multichain@shapeshiftoss/metamask-snaps@shapeshiftoss/hdwallet-metamask@shapeshiftoss/hdwallet-coinbase
4.0.3

3 years ago

4.0.2

3 years ago

4.0.1

3 years ago

4.0.0

3 years ago

3.0.0

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago