Skip to content

Commit

Permalink
lang, docs: ErrorCode docs (#1204)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-schaaf committed Dec 31, 2021
1 parent 69299fb commit 4013ec9
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 15 deletions.
65 changes: 59 additions & 6 deletions lang/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,102 +1,155 @@
use crate::error;

// Error codes that can be returned by internal framework code.
/// Error codes that can be returned by internal framework code.
///
/// - >= 100 Instruction error codes
/// - >= 1000 IDL error codes
/// - >= 2000 constraint error codes
/// - >= 3000 account error codes
/// - = 4000 state error code
/// - = 5000 deprecated error code
///
/// The starting point for user-defined errors is defined
/// by the [ERROR_CODE_OFFSET](crate::__private::ERROR_CODE_OFFSET).
#[error(offset = 0)]
pub enum ErrorCode {
// Instructions.
// Instructions
/// 100 - 8 byte instruction identifier not provided
#[msg("8 byte instruction identifier not provided")]
InstructionMissing = 100,
/// 101 - Fallback functions are not supported
#[msg("Fallback functions are not supported")]
InstructionFallbackNotFound,
/// 102 - The program could not deserialize the given instruction
#[msg("The program could not deserialize the given instruction")]
InstructionDidNotDeserialize,
/// 103 - The program could not serialize the given instruction
#[msg("The program could not serialize the given instruction")]
InstructionDidNotSerialize,

// IDL instructions.
// IDL instructions
/// 1000 - The program was compiled without idl instructions
#[msg("The program was compiled without idl instructions")]
IdlInstructionStub = 1000,
/// 1001 - Invalid program given to the IDL instruction
#[msg("Invalid program given to the IDL instruction")]
IdlInstructionInvalidProgram,

// Constraints.
// Constraints
/// 2000 - A mut constraint was violated
#[msg("A mut constraint was violated")]
ConstraintMut = 2000,
/// 2001 - A has one constraint was violated
#[msg("A has one constraint was violated")]
ConstraintHasOne,
/// 2002 - A signer constraint was violated
#[msg("A signer constraint was violated")]
ConstraintSigner,
/// 2003 - A raw constraint was violated
#[msg("A raw constraint was violated")]
ConstraintRaw,
/// 2004 - An owner constraint was violated
#[msg("An owner constraint was violated")]
ConstraintOwner,
/// 2005 - A rent exemption constraint was violated
#[msg("A rent exemption constraint was violated")]
ConstraintRentExempt,
/// 2006 - A seeds constraint was violated
#[msg("A seeds constraint was violated")]
ConstraintSeeds,
/// 2007 - An executable constraint was violated
#[msg("An executable constraint was violated")]
ConstraintExecutable,
/// 2008 - A state constraint was violated
#[msg("A state constraint was violated")]
ConstraintState,
/// 2009 - An associated constraint was violated
#[msg("An associated constraint was violated")]
ConstraintAssociated,
/// 2010 - An associated init constraint was violated
#[msg("An associated init constraint was violated")]
ConstraintAssociatedInit,
/// 2011 - A close constraint was violated
#[msg("A close constraint was violated")]
ConstraintClose,
/// 2012 - An address constraint was violated
#[msg("An address constraint was violated")]
ConstraintAddress,
/// 2013 - Expected zero account discriminant
#[msg("Expected zero account discriminant")]
ConstraintZero,
/// 2014 - A token mint constraint was violated
#[msg("A token mint constraint was violated")]
ConstraintTokenMint,
/// 2015 - A token owner constraint was violated
#[msg("A token owner constraint was violated")]
ConstraintTokenOwner,
// The mint mint is intentional -> a mint authority for the mint.
/// The mint mint is intentional -> a mint authority for the mint.
///
/// 2016 - A mint mint authority constraint was violated
#[msg("A mint mint authority constraint was violated")]
ConstraintMintMintAuthority,
/// 2017 - A mint freeze authority constraint was violated
#[msg("A mint freeze authority constraint was violated")]
ConstraintMintFreezeAuthority,
/// 2018 - A mint decimals constraint was violated
#[msg("A mint decimals constraint was violated")]
ConstraintMintDecimals,
/// 2019 - A space constraint was violated
#[msg("A space constraint was violated")]
ConstraintSpace,

// Accounts.
/// 3000 - The account discriminator was already set on this account
#[msg("The account discriminator was already set on this account")]
AccountDiscriminatorAlreadySet = 3000,
/// 3001 - No 8 byte discriminator was found on the account
#[msg("No 8 byte discriminator was found on the account")]
AccountDiscriminatorNotFound,
/// 3002 - 8 byte discriminator did not match what was expected
#[msg("8 byte discriminator did not match what was expected")]
AccountDiscriminatorMismatch,
/// 3003 - Failed to deserialize the account
#[msg("Failed to deserialize the account")]
AccountDidNotDeserialize,
/// 3004 - Failed to serialize the account
#[msg("Failed to serialize the account")]
AccountDidNotSerialize,
/// 3005 - Not enough account keys given to the instruction
#[msg("Not enough account keys given to the instruction")]
AccountNotEnoughKeys,
/// 3006 - The given account is not mutable
#[msg("The given account is not mutable")]
AccountNotMutable,
/// 3007 - The given account is owned by a different program than expected
#[msg("The given account is owned by a different program than expected")]
AccountOwnedByWrongProgram,
/// 3008 - Program ID was not as expected
#[msg("Program ID was not as expected")]
InvalidProgramId,
/// 3009 - Program account is not executable
#[msg("Program account is not executable")]
InvalidProgramExecutable,
/// 3010 - The given account did not sign
#[msg("The given account did not sign")]
AccountNotSigner,
/// 3011 - The given account is not owned by the system program
#[msg("The given account is not owned by the system program")]
AccountNotSystemOwned,
/// 3012 - The program expected this account to be already initialized
#[msg("The program expected this account to be already initialized")]
AccountNotInitialized,
/// 3013 - The given account is not a program data account
#[msg("The given account is not a program data account")]
AccountNotProgramData,

// State.
/// 4000 - The given state account does not have the correct address
#[msg("The given state account does not have the correct address")]
StateInvalidAddress = 4000,

// Used for APIs that shouldn't be used anymore.
// Deprecated
/// 5000 - The API being used is deprecated and should no longer be used
#[msg("The API being used is deprecated and should no longer be used")]
Deprecated = 5000,
}
27 changes: 21 additions & 6 deletions lang/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,23 +280,34 @@ pub mod prelude {
}

// Internal module used by macros and unstable apis.
#[doc(hidden)]
pub mod __private {
use solana_program::program_error::ProgramError;
use solana_program::pubkey::Pubkey;
// Modules with useful information for users
// don't use #[doc(hidden)] on these
pub use crate::error::ErrorCode;

#[doc(hidden)]
pub use crate::ctor::Ctor;
pub use crate::error::{Error, ErrorCode};
#[doc(hidden)]
pub use crate::error::Error;
#[doc(hidden)]
pub use anchor_attribute_account::ZeroCopyAccessor;
#[doc(hidden)]
pub use anchor_attribute_event::EventIndex;
#[doc(hidden)]
pub use base64;
#[doc(hidden)]
pub use bytemuck;

#[doc(hidden)]
use solana_program::program_error::ProgramError;
#[doc(hidden)]
use solana_program::pubkey::Pubkey;
#[doc(hidden)]
#[doc(hidden)]
pub mod state {
pub use crate::accounts::state::*;
}

// The starting point for user defined error codes.
/// The starting point for user defined error codes.
pub const ERROR_CODE_OFFSET: u32 = 6000;

// Calculates the size of an account, which may be larger than the deserialized
Expand All @@ -307,11 +318,13 @@ pub mod __private {
}

// Very experimental trait.
#[doc(hidden)]
pub trait ZeroCopyAccessor<Ty> {
fn get(&self) -> Ty;
fn set(input: &Ty) -> Self;
}

#[doc(hidden)]
impl ZeroCopyAccessor<Pubkey> for [u8; 32] {
fn get(&self) -> Pubkey {
Pubkey::new(self)
Expand All @@ -321,7 +334,9 @@ pub mod __private {
}
}

#[doc(hidden)]
pub use crate::accounts::state::PROGRAM_STATE_SEED;
#[doc(hidden)]
pub const CLOSED_ACCOUNT_DISCRIMINATOR: [u8; 8] = [255, 255, 255, 255, 255, 255, 255, 255];
}

Expand Down
15 changes: 12 additions & 3 deletions lang/syn/src/parser/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ pub fn parse(error_enum: &mut syn::ItemEnum, args: Option<ErrorArgs>) -> Error {
};
last_discriminant = id + 1;

// Remove any attributes on the error variant.
variant.attrs = vec![];
// Remove any non-doc attributes on the error variant.
variant.attrs = variant
.attrs
.iter()
.filter(|attr| attr.path.segments[0].ident == "doc")
.cloned()
.collect();

ErrorCode { id, ident, msg }
})
Expand All @@ -40,7 +45,11 @@ pub fn parse(error_enum: &mut syn::ItemEnum, args: Option<ErrorArgs>) -> Error {
}

fn parse_error_attribute(variant: &syn::Variant) -> Option<String> {
let attrs = &variant.attrs;
let attrs = variant
.attrs
.iter()
.filter(|attr| attr.path.segments[0].ident != "doc")
.collect::<Vec<_>>();
match attrs.len() {
0 => None,
1 => {
Expand Down

0 comments on commit 4013ec9

Please sign in to comment.