# Err.decode

Reads revert data as one of the errors you give, or as a plain revert. It never fails.

## Usage

```ts
import { Abi, Err, type Hex } from "@ethect/core";

class ExceededMaxDeposit extends Err.make("ERC4626ExceededMaxDeposit", {
  receiver: Abi.address,
  assets: Abi.uint256,
  max: Abi.uint256,
}) {}

// what a node returned for a call that reverted
const explain = (data: Hex.Hex): string => {
  const error = Err.decode([ExceededMaxDeposit], data);
  return error._tag === "ERC4626ExceededMaxDeposit"
    ? `The vault takes ${error.max} more`
    : error.message;
};
```

You rarely call it yourself: a read and a send decode a revert with the errors of the function
called. It is for revert data you got another way: a trace, an `eth_estimateGas` that failed,
a transaction replayed by hand.

### What the data decodes to

| The data starts with | It decodes to |
| --- | --- |
| the selector of one of the errors | an instance of that error |
| `0x08c379a0`, `Error(string)` | a [`Revert.Reason`](https://ethect.dev/reference/revert/), with the message |
| `0x4e487b71`, `Panic(uint256)` | a [`Revert.Panic`](https://ethect.dev/reference/revert/#panic-codes), with the code |
| anything else, or nothing | a [`Revert.Unknown`](https://ethect.dev/reference/revert/), with the selector and the data |

```ts
import { Err } from "@ethect/core";

// an overflow
const panic = Err.decode([], `0x4e487b71${"11".padStart(64, "0")}`);
// revert(), with nothing
const nothing = Err.decode([], "0x");
// an error that is not among those given
const unknown = Err.decode([], "0xdeadbeef");

panic.message; // "Panic 0x11: an arithmetic operation overflowed or underflowed"
nothing.message; // "Reverted without data"
unknown.message; // "Reverted with data no declared error matches: 0xdeadbeef"
```

### The errors of calls

[`Err.of`](https://ethect.dev/reference/err/of/) gives the errors that calls declare, to decode a revert of one
of them.

```ts
import { Err, type Call, type Hex } from "@ethect/core";

const reasonOf = <E>(calls: ReadonlyArray<Call.Call<unknown, E>>, data: Hex.Hex) =>
  Err.decode(Err.of(calls), data);
```

## Returns

`InstanceType<Errors[number]> | Revert.Revert`

An instance of one of the errors, or one of the three plain reverts. Every one has a `_tag`
and a `message`.

## Parameters

### errors

- **Type:** `ReadonlyArray<new (value: any) => unknown>`

The error classes the data may be. An empty array reads plain reverts only.

### data

- **Type:** [`Hex.Hex`](https://ethect.dev/reference/primitives/hex/)

The revert data.

## Behavior

- **It never fails and never throws.** It is a plain function, not an effect.
- **Data that has the selector of an error and does not decode is a `Revert.Unknown`.** The
  same goes for an `Error(string)` or a `Panic(uint256)` whose data is cut short.
- **The first error that has the selector is the one.** Two classes of one signature decode to
  the first of the list.
- **A class that `Err.make` did not build is skipped.**
- **A selector is compared in lowercase.** Data in uppercase decodes the same.
