Socket
Socket
Sign inDemoInstall

eth-json-rpc-errors

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eth-json-rpc-errors

Ethereum JSON RPC errors and standard JSON RPC 2.0 errors.


Version published
Maintainers
1
Created
Source

eth-json-rpc-errors

Errors for JSON RPC 2.0 and ETH JSON RPC.

Supported Errors

Usage

Installation: npm install eth-json-rpc-errors

Import using ES6 syntax (no default) or Node require.

Errors API

import { errors as rpcErrors } from 'eth-json-rpc-errors'

// standard JSON RPC 2.0 errors namespaced directly under rpcErrors
response.error = rpcErrors.methodNotFound(
  optionalCustomMessage, optionalData
)

// ETH JSON RPC errors namespaced under rpcErrors.eth
response.error = rpcErrors.eth.unauthorized(
  optionalCustomMessage, optionalData
)

// the message can be falsy or a string
// a falsy message will produce an error with a default message
response.error = rpcErrors.eth.unauthorized(null, optionalData)

// omitting the data argument will produce an error without a
// "data" property
response.error = rpcErrors.eth.unauthorized(optionalCustomMessage)

// both arguments can be omitted for almost all errors
response.error = rpcErrors.eth.unauthorized()
response.error = rpcErrors.methodNotFound()

// the JSON RPC 2.0 server error requires a valid code
response.error = rpcErrors.server(
  -32031, optionalCustomMessage, optionalData
)

// there's an option for custom ETH errors
// it requires a valid code and a string message
// valid codes are integers i such that: 1000 <= i <= 4999
response.error = rpcErrors.eth.custom(
  1001, requiredMessage, optionalData
)

Other Exports

/**
 * TypeScript interfaces
 */
import {
  IRpcErrors, IJsonRpcError, IEthJsonRpcError, ISerializeError
} from 'eth-json-rpc-errors/@types'

/**
 * Classes
 */
import { JsonRpcError, EthJsonRpcError } from 'eth-json-rpc-errors'

/**
 * serializeError
 */
// this is useful for ensuring your errors are standardized
import { serializeError } from 'eth-json-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 the 
const fallbackError = { code: 4999, message: 'My custom error.' }
response.error = serializeError(maybeAnError, fallbackError)

// the default fallback is:
{
  code: -32603,
  message: 'Internal JSON-RPC error.'
}

/**
 * getMessageFromCode & ERROR_CODES
 */
import { getMessageFromCode, ERROR_CODES } from 'eth-json-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)

// {
//   jsonRpc: { [errorName]: code, ... },
//   eth: { [errorName]: code, ... },
// }
const code1 = ERROR_CODES.jsonRpc.parse
const code2 = ERROR_CODES.eth.userRejectedRequest

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

License

MIT

Keywords

FAQs

Package last updated on 26 Sep 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc