Detalhes do pacote

eth-rpc-errors

MetaMask2.2mMIT4.0.3

Ethereum RPC and Provider errors.

rpc, ethereum, errors, utility

readme (leia-me)

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

changelog (log de mudanças)

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

Unreleased

[4.0.3] - 2021-03-10

Fixed

  • Correctly type ethErrors getter function argument objects as optional (#36)

4.0.2 - 2020-11-17

Changed

  • Mark the data property of EthereumRpcError and EthereumProviderError as optional rather than T | undefined (#34)

Fixed

  • Correctly type SerializedEthereumRpcError.stack as string, if present (#34)

4.0.1 - 2020-11-03

Changed

  • Updated README.md (#30)

4.0.0 - 2020-11-02

Changed

  • BREAKING: ERROR_CODES export renamed to errorCodes (#28)
  • BREAKING: ethErrors getters will now throw an error if passed a truthy, non-string message (#28)
  • Updated TypeScript types for all exports (#28)

3.0.0 - 2020-07-29

Changed

  • BREAKING: Second argument of serializeError is now an options object (#22)
  • Error stacks are no longer serialized by default by serializeError (#22)

2.1.1 - 2020-05-12

Changed

  • Prevent unnecessary files from being published (#17)

2.1.0 - 2020-05-11

Added

  • New/missing errors:
    • ethErrors.provider (EIP-1193)
      • .disconnected, 4900
      • .chainDisconnected, 4901
    • ethErrors.rpc (EIP-1474)
      • .limitExceeded, -32005

Changed

  • Rename package to eth-rpc-errors

2.0.2 - 2020-02-12

Changed

  • Fix faulty null checks throughout codebase (764832d)

2.0.1 - 2020-01-31

Added

  • Error codes in docstrings (5452001)

[2.0.0] - 2019-09-26

  • Exports
    • errors renamed ethErrors
    • JsonRpcError renamed EthereumRpcError
    • EthJsonRpcError renamed EthereumProviderError
      • It is still a subclass of EthereumRpcError
    • TypeScript
      • Renamed affected interfaces
  • ethErrors
    • Added missing EIP-1474 errors
      • Added corresponding codes and messages
    • Namespacing
      • EIP-1474 (which includes JSON RPC 2.0) errors now namespaced under ethErrors.rpc
        • JSON RPC 2.0 errors were formerly under errors.jsonRpc
      • EIP-1193 errors now namespaced under ethErrors.provider
        • Formerly under errors.eth
    • Most error getters now take a single, optional opts argument, which is either a string or an object
      • If a string, it becomes the error message
      • If an object, it should have the form: { message?: string, data?: any }
      • Special Cases
        • ethErrors.rpc.server must receive a single object of the form:
          • `{ code: number, message?: string, data?: any }
        • ethErrors.provider.custom must receive a single of the form:
          • `{ code: number, message: string, data?: any }
  • TypeScript
    • Updated affected interfaces

[1.1.0] - 2019-09-16

  • serializeError
    • If the object passed to the function has a .message property, it will preferred over the .message property of the fallback error when creating the returned serialized error object