Skip to content

isaacnewton123/viem-error-parser

viem-error-parser

Decode Viem and Wagmi errors into clean, human-readable messages — including custom revert reasons from your own ABIs.

CI npm version Bundle size License: MIT TypeScript

Why

Viem and Wagmi throw deeply-nested error chains. Pulling a useful message out of one — and decoding custom revert MyError(uint256) payloads on top of Error(string) and Panic(uint256) — is verbose and easy to get wrong.

viem-error-parser does the boring part:

  • Walks the cause chain (and errors[]) safely with cycle and depth guards.
  • Extracts revert data from every common shape: data, rawData, returnData, output, or hex embedded in messages.
  • Decodes the two standard Solidity revert types out of the box.
  • Decodes your custom errors when you register your ABIs.
  • Classifies non-revert problems too — user rejection, gas, network, RPC, EIP-1193 codes, and more.
  • Ships ESM + CJS + types, tree-shakable, with a react subpath for a useErrorParser hook.

It is a thin (~22 KB ESM) layer on top of viem. The only runtime dependency is viem itself.

Install

pnpm add viem-error-parser viem

30-second example

import { forViem } from 'viem-error-parser';

const decoder = forViem();

try {
  await walletClient.writeContract({ /* ... */ });
} catch (error) {
  console.error(decoder.decode(error).message);
  // "ERC20InsufficientBalance(sender=0x..., balance=5n, needed=100n)"
  // or "The transaction was rejected by the user."
}

Documentation

The full docs live in ./docs:

A copy of the docs is also published as the GitHub Wiki.

Examples

Three runnable examples live under ./examples:

Contributing

See CONTRIBUTING.md. All contributors follow the Code of Conduct. Security issues should be reported per SECURITY.md.

License

MIT © 2026 Hanif Maulana (Isaac Newton)

Creator

Hanif Maulana (Isaac Newton)

Support

If this library saves you from Viem/Wagmi error chaos, consider supporting development:

About

Decode Viem and Wagmi errors into clean, human-readable messages — including custom revert reasons from your own ABIs.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Contributors