Introduction
Decode Viem and Wagmi errors into clean, human-readable messages — including custom revert reasons from your own ABIs.
Decode Viem and Wagmi errors into clean, human-readable messages — including custom
revertreasons from your own ABIs.
Why viem-error-parser?
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
causechain (anderrors[]) 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
reactsubpath for auseErrorParserhook.
It is a thin (~22 KB ESM) layer on top of viem. The only runtime dependency is viem itself.
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."
}Pages
Installation
Install and peer-dependency notes.
Quick Start
Viem / Wagmi / React in 30 seconds each.
Custom ABIs
Decode revert MyError(...) from your contracts.
Result Shape
ParseResult discriminated union and type guards.
Classifier
Non-revert errors, full rule table, decoder pipeline.
Advanced Usage
AbiRegistry and ErrorDecoder by hand.
TypeScript
Branded types and the zero-runtime ./types entry.
Bundle Size
What tree-shaking actually removes.
API Reference
Every public export.