# Custom errors (Err)

Custom errors of a contract. Each one is a tagged error class, decoded from revert data and
caught by its tag. A function that declares an error fails with it as a typed failure, in a
read and in a send.

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

// error ERC721NonexistentToken(uint256 tokenId)
class NonexistentToken extends Err.make("ERC721NonexistentToken", {
  tokenId: Abi.uint256,
}) {}

// function ownerOf(uint256 tokenId) view returns (address)
const ownerOf = Fn.view<[tokenId: _]>()("ownerOf", {
  inputs: [Abi.uint256],
  output: Abi.address,
  errors: [NonexistentToken],
});

const owner = (nft: Address.Address, tokenId: bigint) =>
  ownerOf.at(nft)(tokenId).pipe(
    Effect.map(Option.some),
    Effect.catchTag("ERC721NonexistentToken", () => Effect.succeed(Option.none())),
  );
```

| Export | Kind | Description |
| --- | --- | --- |
| [`Err.make(name, fields)`](https://ethect.dev/reference/err/make/) | function | A custom error: a class to extend, whose instances hold the fields and have the name as `_tag`. |
| [`Err.decode(errors, data)`](https://ethect.dev/reference/err/decode/) | function | Revert data as one of these errors, or as a plain [`Revert`](https://ethect.dev/reference/revert/). |
| [`Err.of(calls)`](https://ethect.dev/reference/err/of/) | function | The errors that a set of calls declares. |
| `Err.Any` | type | Any custom error class. |

## Where an error is declared

A call fails with the errors its function knows. There are two places to say them.

| Declared | The error is a failure of |
| --- | --- |
| in the `errors` of a [function](https://ethect.dev/reference/fn/view/#errors) | the calls of that function |
| as an entry of a [`Contract`](https://ethect.dev/reference/contract/) | the calls of every function reached through the contract |

Revert data that is none of them is a plain [`Revert`](https://ethect.dev/reference/revert/): a message, a
panic, or data that nothing reads.
