# Errors

The three tagged errors a call can fail with. RpcError when the node answered with an error, TransportError when no answer came back, CodecError when a value does not match its schema.

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.

```ts
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

| Error | Raised when | Tells you |
| --- | --- | --- |
| [`RpcError`](https://ethect.dev/reference/errors/rpc-error/) | the node or wallet answered with a JSON-RPC error | `code`, `message`, `data`, and a `reason` such as `ExecutionReverted` or `UserRejectedRequest` |
| [`TransportError`](https://ethect.dev/reference/errors/transport-error/) | no JSON-RPC answer came back | a `reason`: `Unreachable`, `HttpStatus`, `ConnectionLost`, `MalformedResponse` or `Unserializable` |
| [`CodecError`](https://ethect.dev/reference/errors/codec-error/) | 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`](https://ethect.dev/rpc/method/#types), the error type of every method and
every subscription.

Each is its own Schema: declared in the failure of a query or of a command, it crosses a wire as
its tag and its fields, and is read back as an instance of its class. What a node sent is
written as JSON. The `cause` of a `TransportError` is never written, and the `cause` of a
`CodecError` is written as its name and its message.
[`Transport.Error`](https://ethect.dev/reference/transport/#types) is the Schema of the first two.

## 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](https://ethect.dev/rpc/guides/custom-methods/) with a schema of that shape |

Each situation has an example on the page it belongs to.

| Situation | Its example |
| --- | --- |
| a revert | [`RpcError`, Reverts](https://ethect.dev/reference/errors/rpc-error/#reverts) |
| a refusal | [Talk to a wallet directly](https://ethect.dev/guides/wallets/) |
| a method that is not served, a rate limit | [`RpcError`, Reasons](https://ethect.dev/reference/errors/rpc-error/#reasons) |
| a node that is down, an error page, a socket that closed | [`TransportError`, Reasons](https://ethect.dev/reference/errors/transport-error/#reasons), and [when to retry](https://ethect.dev/reference/errors/transport-error/#retrying) |
| an argument out of range | [`CodecError`](https://ethect.dev/reference/errors/codec-error/) |
| a chain that answers in another shape | [Custom methods](https://ethect.dev/rpc/guides/custom-methods/) |

The [Errors guide](https://ethect.dev/guides/errors/) is about the failures of the main APIs: a contract that
reverts, a user who refuses, and [every failure of the library](https://ethect.dev/guides/errors/#every-failure)
in one table.

## 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 |

```ts
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

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
`SignerRejected`: the [Errors guide](https://ethect.dev/guides/errors/#every-failure) lists them all.

| Error | Raised by | When |
| --- | --- | --- |
| [`AbiError.AbiError`](https://ethect.dev/reference/errors/abi-error/) | [`Abi`](https://ethect.dev/reference/abi/), [`Fn`](https://ethect.dev/reference/fn/), [`Call`](https://ethect.dev/reference/call/) and [`Tx`](https://ethect.dev/reference/tx/) | a value is not what its Solidity type allows, or bytes are not its encoding |
| [`Address.InvalidAddressError`](https://ethect.dev/reference/primitives/address/#invalidaddresserror) | [`Address.make`](https://ethect.dev/reference/primitives/address/#make) | the string is not an address, or its checksum is wrong |
| [`Hex.InvalidHexError`](https://ethect.dev/reference/primitives/hex/#invalidhexerror) | [`Hex.toBytes`](https://ethect.dev/reference/primitives/hex/) | the string is not hex of even length |
| [`AnvilNode.AnvilError`](https://ethect.dev/reference/testing/anvil-node/#anvilerror) | [`AnvilNode.make`](https://ethect.dev/reference/testing/anvil-node/) | anvil could not be started |

`AbiError.AbiError` is its own Schema: it can be declared in the failure of a query or of a
command, and crosses a wire as its tag and its message. `Address.InvalidAddressError` and
`Hex.InvalidHexError` are not: they are what parsing a person's input fails with, and a program
that only parses ships nothing of Schema. `AnvilError` is not either: it is for tests, and what
anvil printed can hold the URL of a fork.

The one function that throws is
[`Address.makeUnsafe`](https://ethect.dev/reference/primitives/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.
