Errors
A call fails with one of three tagged errors: RpcError when the node answered with an error,
TransportError when no answer came back, CodecError when a value does not match its schema.
They are values in the error channel of the Effect. No effect throws.
import { Effect } from "effect";import { Eth } from "@ethect/core/rpc";
const head = Eth.blockNumber().pipe( Effect.map((number) => `block ${number}`), Effect.catchTags({ RpcError: (error) => Effect.succeed(`the node refused: ${error.code}`), TransportError: (error) => Effect.succeed(`no answer: ${error.reason._tag}`), CodecError: (error) => Effect.succeed(`unexpected ${error.on}`), }),);// Effect<string, never, Transport>The three errors
Section titled “The three errors”| Error | Raised when | Tells you |
|---|---|---|
RpcError |
the node or wallet answered with a JSON-RPC error | code, message, data, and a reason such as ExecutionReverted or UserRejectedRequest |
TransportError |
no JSON-RPC answer came back | a reason: Unreachable, HttpStatus, ConnectionLost, MalformedResponse or Unserializable |
CodecError |
the params could not be encoded, or the result has another shape | the method, whether it failed on the "params" or the "result", and the schema error |
Their union is Method.Error, the error type of every method and
every subscription.
Which one, and what to do
Section titled “Which one, and what to do”| Situation | Error | Reason | Usual response |
|---|---|---|---|
| a call or an estimate reverted | RpcError |
ExecutionReverted |
read reason.data |
| the user closed the wallet prompt | RpcError |
UserRejectedRequest |
carry on without the result |
| the node does not serve the method | RpcError |
MethodNotFound, MethodNotSupported or UnsupportedMethod |
fall back to another method |
| the node rate-limits | RpcError |
LimitExceeded |
wait and retry |
| the node is down or the URL is wrong | TransportError |
Unreachable |
retry, or use another endpoint |
| a proxy answered with an error page | TransportError |
HttpStatus |
read reason.status |
| the socket closed mid-call | TransportError |
ConnectionLost |
repeat the call if it is safe to |
| an argument is out of range | CodecError |
on: "params" |
fix the argument: nothing was sent |
| the chain answers in another shape | CodecError |
on: "result" |
define the method with a schema of that shape |
Each situation has an example on the page it belongs to.
| Situation | Its example |
|---|---|
| a revert | RpcError, Reverts |
| a refusal | Talk to a wallet directly |
| a method that is not served, a rate limit | RpcError, Reasons |
| a node that is down, an error page, a socket that closed | TransportError, Reasons, and when to retry |
| an argument out of range | CodecError |
| a chain that answers in another shape | Custom methods |
The Errors guide is about the failures of the main APIs: a contract that reverts, a user who refuses, and every failure of the library in one table.
Catching
Section titled “Catching”Four functions of Effect cover most handling. Each removes what it handles from the error type.
| Function | Handles |
|---|---|
Effect.catchTag("RpcError", handler) |
one of the three errors |
Effect.catchTags({ RpcError, TransportError, CodecError }) |
several of them, one handler each |
Effect.catchReason("RpcError", "ExecutionReverted", handler) |
one reason of an error. The handler receives the reason. |
Effect.catchReasons("RpcError", { MethodNotFound, LimitExceeded }) |
several reasons of an error |
import { Effect } from "effect";import { Address } from "@ethect/core";import { Eth } from "@ethect/core/rpc";
const TO = Address.makeUnsafe("0x5FbDB2315678afecb367f032d93F642f64180aa3");
const result = Eth.call({ to: TO }, "latest").pipe( Effect.catchReason("RpcError", "ExecutionReverted", (reason) => Effect.succeed(reason.data ?? "0x"), ),);Other tagged errors
Section titled “Other tagged errors”Other errors exist outside the call path. They never come out of a method of the JSON-RPC
layer. These four belong to the base. The main APIs have their own, such as TxReverted and
Rejected: the Errors guide lists them all.
| Error | Raised by | When |
|---|---|---|
AbiError.AbiError |
Abi, Fn, Call and Tx |
a value is not what its Solidity type allows, or bytes are not its encoding |
Address.InvalidAddressError |
Address.make |
the string is not an address, or its checksum is wrong |
Hex.InvalidHexError |
Hex.toBytes |
the string is not hex of even length |
AnvilNode.AnvilError |
AnvilNode.make |
anvil could not be started |
The one function that throws is
Address.makeUnsafe: it checks an address you
wrote in your source, when the file loads, so a typo stops the program before it runs anything.