Skip to main content
Anchor provides built-in error codes and supports custom error definitions.

Custom errors

Define custom errors with #[error_code]:
#[error_code]
pub enum ErrorCode {
    #[msg("Invalid authority")]
    InvalidAuthority,
    #[msg("Insufficient funds")]
    InsufficientFunds,
    #[msg("Value exceeds maximum")]
    ValueTooLarge,
}

Error codes

Custom errors start at code 6000:
  • First error: 6000
  • Second error: 6001
  • And so on…

Anchor framework errors

Common built-in errors:
CodeErrorDescription
100InstructionMissing8 byte instruction identifier not provided
101InstructionFallbackNotFoundFallback functions are not supported
102InstructionDidNotDeserializeThe program could not deserialize the given instruction
103InstructionDidNotSerializeThe program could not serialize the given instruction
140ConstraintMutA mut constraint was violated
141ConstraintHasOneA has_one constraint was violated
142ConstraintSignerA signer constraint was violated
143ConstraintRawA raw constraint was violated
144ConstraintOwnerAn owner constraint was violated
2000AccountDiscriminatorAlreadySetThe account discriminator is already set
2001AccountDiscriminatorNotFoundNo 8 byte discriminator was found
2002AccountDiscriminatorMismatch8 byte discriminator did not match
2003AccountDidNotDeserializeFailed to deserialize the account
2004AccountDidNotSerializeFailed to serialize the account
3000DeclaredProgramIdMismatchThe declared program id does not match
See the complete error list in the source code.

Using errors

pub fn update(ctx: Context<Update>) -> Result<()> {
    require!(
        ctx.accounts.data.authority == ctx.accounts.user.key(),
        ErrorCode::InvalidAuthority
    );
    Ok(())
}
See Error Handling for more details.

Build docs developers (and LLMs) love