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()
throw ethErrors.provider.unauthorized('my custom message')
Supported Errors
- Ethereum JSON RPC
- Ethereum Provider 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'
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-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 { EthereumRpcError, EthereumProviderError } from 'eth-rpc-errors'
import { getMessageFromCode, errorCodes } from 'eth-rpc-errors'
const message1 = getMessageFromCode(someCode)
const message2 = getMessageFromCode(someCode, myFallback)
const message3 = getMessageFromCode(someCode, null)
const code1 = errorCodes.rpc.parse
const code2 = errorCodes.provider.userRejectedRequest
const message4 = getMessageFromCode(code1)
const message5 = getMessageFromCode(code2)
License
MIT