CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-web3-eth-abi

Web3 module encode and decode EVM in/output.

Pending
Overview
Eval results
Files

error-handling.mddocs/

Error Handling

Contract error signature encoding and comprehensive error data decoding with support for EIP-838 execution errors. Essential for handling smart contract errors and providing meaningful error messages.

Capabilities

Encode Error Signature

Encodes the error name to its ABI signature, which is the sha3 hash of the error name including input types.

/**
 * Encodes the error name to its ABI signature (sha3 hash)
 * @param functionName - The error name to encode or the AbiErrorFragment object
 * @returns The ABI signature of the error (32 bytes)
 */
function encodeErrorSignature(functionName: string | AbiErrorFragment): string;

Usage Examples:

import { encodeErrorSignature } from "web3-eth-abi";

// Using error name string
const signature1 = encodeErrorSignature('InsufficientBalance(uint256,uint256)');
console.log(signature1);
// 0x...hash of error signature

// Using JSON interface object
const signature2 = encodeErrorSignature({
  name: "InsufficientBalance",
  type: "error",
  inputs: [
    { type: "uint256", name: "available" },
    { type: "uint256", name: "required" }
  ]
});
console.log(signature2);
// Same hash as above

// Simple error with no parameters
const signature3 = encodeErrorSignature({
  name: "Paused",
  type: "error",
  inputs: []
});
console.log(signature3);
// Hash of "Paused()"

Decode Contract Error Data

Decodes contract error data using error ABI fragments. Supports custom errors defined in smart contracts as well as standard errors like "Error(string)" and "Panic(uint256)".

/**
 * Decodes contract error data using error ABI fragments
 * @param errorsAbi - Array of error ABI fragments to match against
 * @param error - EIP-838 execution error with data field to decode
 * @returns Modifies the error object with decoded properties
 */
function decodeContractErrorData(
  errorsAbi: AbiErrorFragment[],
  error: Eip838ExecutionError
): void;

Usage Examples:

import { decodeContractErrorData } from "web3-eth-abi";

// Define error ABI fragments
const errorAbi = [
  {
    name: "InsufficientBalance",
    type: "error",
    inputs: [
      { name: "available", type: "uint256" },
      { name: "required", type: "uint256" }
    ]
  },
  {
    name: "InvalidAddress",
    type: "error", 
    inputs: [
      { name: "addr", type: "address" }
    ]
  },
  {
    name: "Paused",
    type: "error",
    inputs: []
  }
];

// Mock error object (would come from web3 transaction)
const executionError = {
  message: "Transaction reverted",
  data: "0x12345678000000000000000000000000000000000000000000000000000000000000006400000000000000000000000000000000000000000000000000000000000000c8", // encoded error data
  setDecodedProperties: function(name, signature, args) {
    this.errorName = name;
    this.errorSignature = signature;
    this.errorArgs = args;
  }
};

// Decode the error data
decodeContractErrorData(errorAbi, executionError);

console.log(executionError.errorName); // "InsufficientBalance"
console.log(executionError.errorSignature); // "InsufficientBalance(uint256,uint256)"
console.log(executionError.errorArgs); 
// {
//   '0': 100n,
//   '1': 200n,
//   __length__: 2,
//   available: 100n,
//   required: 200n
// }

Standard Error Handling

The decodeContractErrorData function automatically handles standard Ethereum errors even when no ABI is provided:

Error(string) - 0x08c379a0

Standard revert with string message.

// Error with signature 0x08c379a0... will be decoded as:
// {
//   errorName: "Error",
//   errorSignature: "Error(string)", 
//   errorArgs: { '0': "error message", __length__: 1, message: "error message" }
// }

Panic(uint256) - 0x4e487b71

Panic errors with numeric codes.

// Error with signature 0x4e487b71... will be decoded as:
// {
//   errorName: "Panic",
//   errorSignature: "Panic(uint256)",
//   errorArgs: { '0': 18n, __length__: 1, code: 18n } // 18 = arithmetic overflow
// }

Common Panic Codes

  • 0x01: Assert false
  • 0x11: Arithmetic overflow/underflow
  • 0x12: Division by zero
  • 0x21: Invalid enum value
  • 0x22: Invalid storage byte array access
  • 0x31: Pop on empty array
  • 0x32: Array out of bounds access
  • 0x41: Out of memory
  • 0x51: Invalid function selector

Error Processing Flow

  1. Extract error signature (first 4 bytes) from error data
  2. Match signature against provided error ABI fragments
  3. If match found, decode parameters using the error's input specification
  4. If no match, check for standard errors (Error/Panic)
  5. Set decoded properties on the error object

Types

interface AbiErrorFragment {
  type: "error";
  name?: string;
  inputs?: AbiInput[];
}

interface AbiInput {
  name?: string;
  type: string;
  components?: AbiInput[];
}

interface Eip838ExecutionError extends Error {
  data?: string;
  setDecodedProperties(
    errorName: string,
    errorSignature?: string,
    errorArgs?: { [K in string]: unknown }
  ): void;
}

Install with Tessl CLI

npx tessl i tessl/npm-web3-eth-abi

docs

eip-712.md

error-handling.md

event-processing.md

function-operations.md

index.md

parameter-processing.md

tile.json