Errors in Solana

Errors in Solana can be difficult to interpret. When you get a custom error, have to lookup the error code yourself.

SolanaError: custom program error: #3002

Would be

/// 3002 - Account discriminator did not match what was expected
#[msg("Account discriminator did not match what was expected")]
AccountDiscriminatorMismatch,

Error codes that can be returned by internal framework code are identified by its number range:

  • = 100 Instruction error codes

  • = 1000 IDL error codes

  • = 2000 constraint error codes

  • = 3000 account error codes

  • = 4100 misc error codes

  • = 5000 deprecated error code

User defined errors are anything greater than 6000.

Custom errors

You can define your own custom errors in a module. They increment from 6000 by their order of definition.

use anchor_lang::prelude::*;

#[error_code]
pub enum CustomError {
  // 6000
  #[msg("Maths not mathing")]
  MathOverflow,

// 60001
  #[msg("Maths still not mathing")]
  MathUnderflow,
}

Token gotcha

I am already using InterfaceAccount, I added the constraint associated_token::mint = mint but it gives An associated constraint was violated when i use a Token2022 mint

use associated_token::token_program = token_program

Out of room on the stack

"Program D4JCMSe8bh1GcuPyGjicJ4JbdcmWmAPLvcuDqgpVSWFB failed: Access violation in unknown section at address 0x109 of size 8"

You ran out of room on the stack. Box your accounts to move them to the heap.

Too many instruction parameters

InstructionDidNotDeserialize

You can't have infinite parameters.

Corrupt data on chain

pub struct InstrData {
    pub decimals: u8,
    pub weights: [u32; 10],
    pub inv_T: u128,
}

This struct is missing #[derive(AnchorSerialize, AnchorDeserialize)] so if you try and pass this to an instruction the data won't be what you expect.