Skip to content

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.

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) function A custom error: a class to extend, whose instances hold the fields and have the name as _tag.
Err.decode(errors, data) function Revert data as one of these errors, or as a plain Revert.
Err.of(calls) function The errors that a set of calls declares.
Err.Any type Any custom error class.

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 the calls of that function
as an entry of a Contract the calls of every function reached through the contract

Revert data that is none of them is a plain Revert: a message, a panic, or data that nothing reads.