Skip to content

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.

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 holds the fields, the tag, and a message that writes the error with its values.

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 give you the instance a revert decodes to.

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

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: compare its selector with the one of the error.

An error without fields takes an empty object.

import { Err } from "@ethect/core";
// error Unauthorized()
class Unauthorized extends Err.make("Unauthorized", {}) {}

Err.Class<Name, F>

A class, and its Schema. An instance is an Err.Instance<Name, F>: an Error with the fields of the error as properties, and 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
Type Is
Err.Class<Name, F> the class: new (value) => Err.Instance<Name, F>, a Schema.Class, signature and selector
Err.Instance<Name, F> YieldableError & Fields & { readonly _tag: Name }: what decoding gives, typed. At run time it is an instance of your class
Err.Listed any custom error class, as a Schema that needs no service: what Tx.Reverted.schema takes

The class is its own Schema. Declared in the failure of a query or of a command, an error is written as its tag and its fields, and read back as an instance of your class, with its message.

import { Schema } from "effect";
import { Abi, Err, type Address } from "@ethect/core";
class InsufficientBalance extends Err.make("ERC20InsufficientBalance", {
sender: Abi.address,
balance: Abi.uint256,
needed: Abi.uint256,
}) {}
const Json = Schema.toCodecJson(InsufficientBalance);
declare const sender: Address.Address;
const error = new InsufficientBalance({ sender, balance: 0n, needed: 1n });
const written = Schema.encodeSync(Json)(error);
// { _tag: "ERC20InsufficientBalance", sender: "0xf39F…2266", balance: "0", needed: "1" }
const read = Schema.decodeUnknownSync(Json)(written);
read instanceof InsufficientBalance; // true
read.needed; // 1n

A field is written by its Solidity type:

Field Plain Schema.encode In JSON, through Schema.toCodecJson
an integer: uint256, int8, uint48… a bigint a decimal string: "1000000"
address text, in EIP-55 the same
bool, string, bytes, bytesN as the value the same
a list, a struct each item by its type the same

Effect’s RPC and HTTP API write JSON through Schema.toCodecJson. Plain Schema.encode gives a bigint, which JSON.stringify refuses: write JSON yourself through Schema.toCodecJson too.

  • An address is read in any case. It is written in EIP-55, however deep in a list or a struct, as every address the package writes is. Read back, it is lowercase, as a decoded revert holds it.
  • Reading checks every field. An integer out of the range of its type, an address that is not 20 bytes, a number where a decimal string is due: the error is not read, and the failure is a SchemaError.
  • The message is not written. It is made from the fields, on either side. A field named message is written, as any field is.
  • Type: string

The name of the error in the contract.

  • Type: Abi.Fields

The Solidity 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.

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 }) {}
  • new checks nothing. An instance is built from decoded data without a check that could throw: with fields of their types, it never throws. 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 dropped. It is decoded from revert data and kept nowhere: the tag is the name of the error, and the field is not written. 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. Nothing throws.