# Reverts (Revert)

What a call reverts with when it is none of the errors its function declares: a message, a
panic, or data that nothing reads. Each is a tagged error with a `message`. A read fails with
one of them, and a send reports it as the `reason` of [`Tx.Reverted`](https://ethect.dev/reference/tx/failures/#txreverted).

Each tag is the module's name, then the export's: `Revert.Unknown` is `"RevertUnknown"`, as
`Tx.Reverted` is `"TxReverted"`.

```ts
import { Effect } from "effect";
import { Erc20, type Address } from "@ethect/core";

const balance = (token: Address.Address, owner: Address.Address) =>
  Erc20.at(token).balanceOf(owner).pipe(
    Effect.catchTag("RevertReason", (revert) => Effect.succeed(revert.message)), // require(…, "paused")
    Effect.catchTag("RevertPanic", (panic) => Effect.succeed(panic.code)), // 0x11: an overflow
    Effect.catchTag("RevertUnknown", (unknown) => Effect.succeed(unknown.data)),
  );
```

| Export | Kind | `_tag` | Description |
| --- | --- | --- | --- |
| [`Revert.Reason`](#properties) | error | `"RevertReason"` | `Error(string)`: a `require` or a `revert` with a message, which is its `message`. |
| [`Revert.Panic`](#panic-codes) | error | `"RevertPanic"` | `Panic(uint256)`: an assertion, an overflow, a division by zero. `code` says which. |
| [`Revert.Unknown`](#what-is-unknown) | error | `"RevertUnknown"` | Revert data that no declared error matches, with its `selector` and its `data`. |
| `Revert.Revert` | type | | Any of the three. |

## Properties

| Error | Property | Type | Holds |
| --- | --- | --- | --- |
| `Revert.Reason` | `message` | `string` | the message the contract gave |
| `Revert.Panic` | `code` | `number` | the code of the panic |
| `Revert.Panic` | `message` | `string` | the code and what it means |
| `Revert.Unknown` | `selector` | `Hex.Hex` | the first four bytes of the data, or `"0x"` when there are fewer |
| `Revert.Unknown` | `data` | `Hex.Hex` | the revert data, whole |
| `Revert.Unknown` | `message` | `string` | `Reverted without data`, or the data no declared error matches |

[`Err.decode`](https://ethect.dev/reference/err/decode/) reads revert data as one of these, when it is none of
the errors you give it.

## As a Schema

Each class is its own Schema. Declared in the failure of a query or of a command, a revert is
written as its tag and its fields, and read back as an instance of its class.

| The error | Written as |
| --- | --- |
| `new Revert.Reason({ message: "paused" })` | `{ "_tag": "RevertReason", "message": "paused" }` |
| `new Revert.Panic({ code: 0x11 })` | `{ "_tag": "RevertPanic", "code": 17 }` |
| `new Revert.Unknown({ selector: "0xdeadbeef", data: "0xdeadbeef00" })` | `{ "_tag": "RevertUnknown", "selector": "0xdeadbeef", "data": "0xdeadbeef00" }` |

The `message` of a panic and of an unknown revert is not written: it is made from the fields,
on either side. `new` checks nothing: with fields of their types, it never throws.

## What is unknown

A `Revert.Unknown` is not always a mystery. The data may be a custom error that the function
called does not declare.

- Declare it in the [`errors`](https://ethect.dev/reference/fn/view/#errors) of the function, or as an entry of
  its [`Contract`](https://ethect.dev/reference/contract/): the read then fails with the error itself.
- Read it afterwards with [`Err.decode`](https://ethect.dev/reference/err/decode/) and the errors it may be:
  `unknown.data` is what to pass.
- Data that is `0x` is a `revert()` with nothing, or a revert the node gave no data for.

## Panic codes

The `message` of a `Revert.Panic` names its code.

| `code` | Message |
| --- | --- |
| `0x00` | `Panic 0x00: a panic the compiler inserted` |
| `0x01` | `Panic 0x01: an assertion failed` |
| `0x11` | `Panic 0x11: an arithmetic operation overflowed or underflowed` |
| `0x12` | `Panic 0x12: a division or a modulo by zero` |
| `0x21` | `Panic 0x21: a value is not a member of its enum` |
| `0x22` | `Panic 0x22: a storage byte array is incorrectly encoded` |
| `0x31` | `Panic 0x31: pop() on an empty array` |
| `0x32` | `Panic 0x32: an index is out of bounds` |
| `0x41` | `Panic 0x41: too much memory was allocated` |
| `0x51` | `Panic 0x51: a call to a function variable that was never set` |
