Skip to content

Reverts (Revert)

What a call reverts with when it is none of the errors its function declares: a message, a panic, or data that nothing reads. Each is a tagged error with a message. A read fails with one of them, and a send reports it as the reason of Tx.Reverted.

Each tag is the module’s name, then the export’s: Revert.Unknown is "RevertUnknown", as Tx.Reverted is "TxReverted".

import { Effect } from "effect";
import { Erc20, type Address } from "@ethect/core";
const balance = (token: Address.Address, owner: Address.Address) =>
Erc20.at(token).balanceOf(owner).pipe(
Effect.catchTag("RevertReason", (revert) => Effect.succeed(revert.message)), // require(…, "paused")
Effect.catchTag("RevertPanic", (panic) => Effect.succeed(panic.code)), // 0x11: an overflow
Effect.catchTag("RevertUnknown", (unknown) => Effect.succeed(unknown.data)),
);
Export Kind _tag Description
Revert.Reason error "RevertReason" Error(string): a require or a revert with a message, which is its message.
Revert.Panic error "RevertPanic" Panic(uint256): an assertion, an overflow, a division by zero. code says which.
Revert.Unknown error "RevertUnknown" Revert data that no declared error matches, with its selector and its data.
Revert.Revert type Any of the three.
Error Property Type Holds
Revert.Reason message string the message the contract gave
Revert.Panic code number the code of the panic
Revert.Panic message string the code and what it means
Revert.Unknown selector Hex.Hex the first four bytes of the data, or "0x" when there are fewer
Revert.Unknown data Hex.Hex the revert data, whole
Revert.Unknown message string Reverted without data, or the data no declared error matches

Err.decode reads revert data as one of these, when it is none of the errors you give it.

A Revert.Unknown is not always a mystery. The data may be a custom error that the function called does not declare.

  • Declare it in the errors of the function, or as an entry of its Contract: the read then fails with the error itself.
  • Read it afterwards with Err.decode and the errors it may be: unknown.data is what to pass.
  • Data that is 0x is a revert() with nothing, or a revert the node gave no data for.

The message of a Revert.Panic names its code.

code Message
0x00 Panic 0x00: a panic the compiler inserted
0x01 Panic 0x01: an assertion failed
0x11 Panic 0x11: an arithmetic operation overflowed or underflowed
0x12 Panic 0x12: a division or a modulo by zero
0x21 Panic 0x21: a value is not a member of its enum
0x22 Panic 0x22: a storage byte array is incorrectly encoded
0x31 Panic 0x31: pop() on an empty array
0x32 Panic 0x32: an index is out of bounds
0x41 Panic 0x41: too much memory was allocated
0x51 Panic 0x51: a call to a function variable that was never set