# Err.make

Defines a custom error of a contract. The result is a class to extend, and its instances are
failures caught by their tag.

## Usage

```ts
import { Effect } from "effect";
import { Abi, Err, Fn, type Address, type _ } from "@ethect/core";

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

// function previewDeposit(uint256 assets) view returns (uint256)
const previewDeposit = Fn.view<[assets: _]>()("previewDeposit", {
  inputs: [Abi.uint256],
  output: Abi.uint256,
  errors: [ExceededMaxDeposit],
});

const shares = (vault: Address.Address, assets: bigint) =>
  previewDeposit
    .at(vault)(assets)
    .pipe(
      // the read failed with the error: its fields are typed
      Effect.catchTag("ERC4626ExceededMaxDeposit", (error) =>
        previewDeposit.at(vault)(error.max),
      ),
    );
```

Extend the result, so the error has a name of its own in your types. The first argument is the
name the contract gives it: it is the `_tag` of the instances, and the selector is hashed from
it.

### An instance

An instance holds the fields, the tag, and a `message` that writes the error with its values.

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

class InsufficientBalance extends Err.make("ERC20InsufficientBalance", {
  sender: Abi.address,
  balance: Abi.uint256,
  needed: Abi.uint256,
}) {}

declare const sender: Address.Address;
const error = new InsufficientBalance({ sender, balance: 0n, needed: 1n });
error._tag; // "ERC20InsufficientBalance"
error.needed; // 1n
error.message; // "ERC20InsufficientBalance(sender: 0xf39f…2266, balance: 0, needed: 1)"
```

You rarely build one: a read, a send and [`Err.decode`](https://ethect.dev/reference/err/decode/) give you the
instance a revert decodes to.

### What an error is known by

The class says the signature of the error, and the selector hashed from it: the four bytes
its revert data starts with.

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

class InsufficientBalance extends Err.make("ERC20InsufficientBalance", {
  sender: Abi.address,
  balance: Abi.uint256,
  needed: Abi.uint256,
}) {}

InsufficientBalance.signature; // "ERC20InsufficientBalance(address,uint256,uint256)"
InsufficientBalance.selector; // "0xe450d38c"
```

Use them to check a definition against a contract, or against revert data that came back as a
[`Revert.Unknown`](https://ethect.dev/reference/revert/#what-is-unknown): compare its `selector` with the one of
the error.

### No field

An error without fields takes an empty object.

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

// error Unauthorized()
class Unauthorized extends Err.make("Unauthorized", {}) {}
```

## Returns

`(new (value) => YieldableError & Fields & { readonly _tag: Name }) & { signature, selector }`

A class. An instance is an `Error` with the fields of the error as properties, and it is a
failure of an `Effect`: `yield*` on one fails with it.

| On the class | Type | Is |
| --- | --- | --- |
| `signature` | `string` | the name of the error and the types of its fields: `ERC20InsufficientBalance(address,uint256,uint256)` |
| `selector` | `Hex.Hex` | the first four bytes of the hash of the signature: what its revert data starts with |

## Parameters

### name

- **Type:** `string`

The name of the error in the contract.

### fields

- **Type:** `Abi.Fields`

The [Solidity types](https://ethect.dev/reference/abi/types/) of the fields, by name, in the order the contract
declares them. The names are yours: only the order and the types are in the selector.

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

// @ts-expect-error a field is named by a Solidity identifier
class Broken extends Err.make("Broken", { 0: Abi.uint256 }) {}
```

## Behavior

- **`new` checks nothing.** An instance is built from decoded data without a check that could
  throw. A value out of range is only refused when it is encoded.
- **A field named `message` is the message.** Such an error has the text the contract gave it.
  Without one, the message is the error written with its values.
- **A field named `_tag` is hidden by the tag.** Give it another name: only the order and the
  types of the fields are in the selector.
- **The tag is the name of the error, not the name of your class.** Two errors of one name
  have one tag: `Effect.catchTag` takes them both.
- **An error that is not a definition decodes nothing.** A field that is no Solidity type, or
  that is named by a number, makes every revert of that selector a
  [`Revert.Unknown`](https://ethect.dev/reference/revert/). Nothing throws.
