npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

eth-rpc-errors

v4.0.3

Published

Ethereum RPC and Provider errors.

Downloads

1,790,129

Readme

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