Error Handling Basics
10 examples to get you started with Errors & Logs - 7 basic and 3 intermediate.
Search across all documentation pages
10 examples to get you started with Errors & Logs - 7 basic and 3 intermediate.
ProgramResult from Rust for Solana.Instructions return Result<(), ProgramError>.
use solana_program::entrypoint::ProgramResult;
pub fn handler() -> ProgramResult { Ok(()) }? in helpers.Runtime provides standard error codes.
return Err(ProgramError::InvalidArgument);Domain-specific u32 codes.
return Err(ProgramError::Custom(6001));thiserror-style From impl.
impl From<MyError> for ProgramError {
fn from(e: MyError) -> Self { ProgramError::Custom(e as u32) }
}? ergonomics.Never unwrap user input paths.
// BAD: data[0]; // GOOD: data.get(0).ok_or(...)?Bubble callee failures.
invoke(&ix, accounts)?;Failed tx with code in metadata.
// off-chain: parse logs + err codeerror_code attribute in 0.32.1.
#[error_code]
pub enum Error { InsufficientFunds }Related: Custom Error Enums
Short msg for debug - mind CU.
solana_program::msg!("reject: insufficient");Related: Logging with msg!
Match Custom code to enum.
// TS: parse simulation errRelated: Decoding Errors Off-Chain
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