eth-json-rpc-errors
Errors for the
Ethereum JSON RPC
and
Ethereum Provider,
and making unknown errors compliant with either spec.
Basic Usage
import { ethErrors } from 'eth-json-rpc-errors'
throw ethErrors.provider.unauthorized()
throw ethErrors.provider.unauthorized('my custom message')
Supported Errors
- Ethereum JSON RPC
- Ethereum Provider errors
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
import { ethErrors } from 'eth-json-rpc-errors'
response.error = ethErrors.rpc.methodNotFound({
message: optionalCustomMessage, data: optionalData
})
response.error = ethErrors.provider.unauthorized({
message: optionalCustomMessage, data: optionalData
})
response.error = ethErrors.provider.unauthorized(customMessage)
response.error = ethErrors.provider.unauthorized()
response.error = ethErrors.provider.unauthorized({})
response.error = ethErrors.rpc.server({
code: -32031
})
response.error = ethErrors.provider.custom({
code: 1001, message: 'foo'
})
Parsing Unknown Errors
import { serializeError } from 'eth-json-rpc-errors'
response.error = serializeError(maybeAnError)
const fallbackError = { code: 4999, message: 'My custom error.' }
response.error = serializeError(maybeAnError, fallbackError)
{
code: -32603,
message: 'Internal JSON-RPC error.'
}
Other Exports
import {
IEthErrors, IEthereumRpcError, IEthereumProviderError, ISerializeError,
IErrorOptions, IRpcServerErrorOptions, IProviderCustomErrorOptions
} from 'eth-json-rpc-errors/@types'
import { EthereumRpcError, EthereumProviderError } from 'eth-json-rpc-errors'
import { getMessageFromCode, ERROR_CODES } from 'eth-json-rpc-errors'
const message1 = getMessageFromCode(someCode)
const message2 = getMessageFromCode(someCode, myFallback)
const message3 = getMessageFromCode(someCode, null)
const code1 = ERROR_CODES.rpc.parse
const code2 = ERROR_CODES.provider.userRejectedRequest
const message4 = getMessageFromCode(code1)
const message5 = getMessageFromCode(code2)
License
MIT