Custom Error Enums
Custom error enums translate business failures into stable u32 codes. Anchor publishes them in IDL; native programs should ship equivalent JSON for @solana/kit 7.0.0 clients.
Search across all documentation pages
Custom error enums translate business failures into stable u32 codes. Anchor publishes them in IDL; native programs should ship equivalent JSON for @solana/kit 7.0.0 clients.
#[repr(u32)]
pub enum VaultError {
InsufficientFunds = 6000,
SlippageExceeded = 6001,
}When to reach for this:
#[error_code]
pub enum Error {
#[msg("Insufficient funds")]
InsufficientFunds,
#[msg("Slippage exceeded")]
SlippageExceeded,
}What this demonstrates:
Clients decode name + code.
return Err(Error::InsufficientFunds.into());| Alternative | Use When | Don't Use When |
|---|---|---|
| Builtin ProgramError only | Internal programs | UX |
| String errors | Not on-chain | N/A |
| Events for failures | Wrong tool | Use errors |
Metadata for IDL.
Hand-write or Codama.
Map code to locale off-chain.
Returns same custom code.
May see callee code in logs.
Practical limit smaller.
Host cfg tests.
All branches return Err not panic.
Anchor convention.
Program id + code tuple.
Tag releases.
@solana/kit 7.0.0 helpers.
Stack versions: This page was written for Agave 4.1.1, Solana CLI 3.0.10, Anchor 0.32.1, anchor-lang 0.32.1, Rust 1.91.1, @solana/kit 7.0.0, Surfpool 0.12.0, and LiteSVM 0.6.x.
Reviewed by Chris St. John·Last updated Jul 16, 2026