# eth-json-rpc-errors

> Ethereum JSON RPC and Provider errors.

Latest version **2.0.2** (published 2020-02-12) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install eth-json-rpc-errors
pnpm add eth-json-rpc-errors
yarn add eth-json-rpc-errors
bun add eth-json-rpc-errors
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 2.0.2 |
| Published | 2020-02-12 |
| First published | 2019-08-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 41.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Erik Marks |
| Maintainers | rekmarks |
| Keywords | json, rpc, ethereum, errors, utility |

## Links

- npm: https://www.npmjs.com/package/eth-json-rpc-errors
- Repository: https://github.com/MetaMask/eth-json-rpc-errors
- Homepage: https://github.com/MetaMask/eth-json-rpc-errors#readme
- Issues: https://github.com/MetaMask/eth-json-rpc-errors/issues
- npm.io page: https://npm.io/package/eth-json-rpc-errors

## Dependencies (1)

- [fast-safe-stringify](https://npm.io/package/fast-safe-stringify.md) ^2.0.6

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 2.0.2 (latest) — 2020-02-12
- 2.0.1 — 2020-01-31
- 2.0.0 — 2019-09-26
- 1.1.1 — 2019-09-26
- 1.1.0 — 2019-09-16
- 1.0.1 — 2019-08-17
- 1.0.0 — 2019-08-17
- 0.3.1 — 2019-08-09
- 0.3.0 — 2019-08-09
- 0.2.0 — 2019-08-08

## README

# eth-json-rpc-errors

Errors for the
[Ethereum JSON RPC](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1474.md)
and
[Ethereum Provider](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1193.md),
and [making unknown errors compliant with either spec](#parsing-unknown-errors).

## Basic Usage

```js
import { ethErrors } from 'eth-json-rpc-errors'

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

## Supported Errors

- Ethereum JSON RPC
  - Per [EIP 1474](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1474.md#error-codes)
    - This includes all
    [JSON RPC 2.0 errors](https://www.jsonrpc.org/specification#error_object)
- Ethereum Provider errors
  - Per [EIP 1193](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1193.md#error-object-and-codes)
    - Does **not** yet support [`CloseEvent` errors or status codes](https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent#Status_codes).

## Usage

Installation: `npm install eth-json-rpc-errors` or `yarn add eth-json-rpc-errors`

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

### Errors API

```js
import { ethErrors } from 'eth-json-rpc-errors'

// Ethereum RPC errors are namespaced under "ethErrors.rpc"
response.error = ethErrors.rpc.methodNotFound({
  message: optionalCustomMessage, data: optionalData
})

// ETH JSON RPC 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

```js
// 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 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

```js
/**
 * TypeScript interfaces
 */
import {
  // these describe to the corresponding exports from index.js
  IEthErrors, IEthereumRpcError, IEthereumProviderError, ISerializeError,
  // these describe the options argument to error getters in ethErrors
  IErrorOptions, IRpcServerErrorOptions, IProviderCustomErrorOptions
} from 'eth-json-rpc-errors/@types'

/**
 * Classes
 */
import { EthereumRpcError, EthereumProviderError } from 'eth-json-rpc-errors'

/**
 * 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.rpc.parse
const code2 = ERROR_CODES.provider.userRejectedRequest

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

## License

MIT

---
_Source: https://npm.io/package/eth-json-rpc-errors · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
