Skip to main content

anchor_lang_error/
lib.rs

1use {
2    anchor_attribute_error::error_code,
3    solana_program_error::ProgramError,
4    solana_pubkey::Pubkey,
5    std::{
6        fmt::{Debug, Display},
7        num::TryFromIntError,
8    },
9};
10
11/// The starting point for user defined error codes.
12pub const ERROR_CODE_OFFSET: u32 = 6000;
13
14// `#[error_code]` macro below accesses `anchor_lang::error::...` so set up
15// a fake module for it.
16mod anchor_lang {
17    pub(crate) mod error {
18        pub(crate) use crate::{AnchorError, Error};
19    }
20}
21
22/// Error codes that can be returned by internal framework code.
23///
24/// - >= 100 Instruction error codes
25/// - >= 1000 IDL error codes
26/// - >= 2000 constraint error codes
27/// - >= 3000 account error codes
28/// - >= 4100 misc error codes
29/// - = 5000 deprecated error code
30///
31/// The starting point for user-defined errors is defined
32/// by the [ERROR_CODE_OFFSET](crate::error::ERROR_CODE_OFFSET).
33#[error_code(offset = 0)]
34pub enum ErrorCode {
35    // Instructions
36    /// 100 - Instruction discriminator not provided
37    #[msg("Instruction discriminator not provided")]
38    InstructionMissing = 100,
39    /// 101 - Fallback functions are not supported
40    #[msg("Fallback functions are not supported")]
41    InstructionFallbackNotFound,
42    /// 102 - The program could not deserialize the given instruction
43    #[msg("The program could not deserialize the given instruction")]
44    InstructionDidNotDeserialize,
45    /// 103 - The program could not serialize the given instruction
46    #[msg("The program could not serialize the given instruction")]
47    InstructionDidNotSerialize,
48
49    // IDL instructions (deprecated — use Program Metadata instead)
50    /// 1000 - The program was compiled without idl instructions
51    #[msg("The program was compiled without idl instructions")]
52    IdlInstructionStub = 1000,
53    /// 1001 - Invalid program given to the IDL instruction
54    #[msg("Invalid program given to the IDL instruction")]
55    IdlInstructionInvalidProgram,
56    /// 1002 - IDL Account must be empty in order to resize
57    #[msg("IDL account must be empty in order to resize, try closing first")]
58    IdlAccountNotEmpty,
59
60    // Event instructions
61    /// 1500 - The program was compiled without `event-cpi` feature
62    #[msg("The program was compiled without `event-cpi` feature")]
63    EventInstructionStub = 1500,
64
65    // Constraints
66    /// 2000 - A mut constraint was violated
67    #[msg("A mut constraint was violated")]
68    ConstraintMut = 2000,
69    /// 2001 - A has one constraint was violated
70    #[msg("A has one constraint was violated")]
71    ConstraintHasOne,
72    /// 2002 - A signer constraint was violated
73    #[msg("A signer constraint was violated")]
74    ConstraintSigner,
75    /// 2003 - A raw constraint was violated
76    #[msg("A raw constraint was violated")]
77    ConstraintRaw,
78    /// 2004 - An owner constraint was violated
79    #[msg("An owner constraint was violated")]
80    ConstraintOwner,
81    /// 2005 - A rent exemption constraint was violated
82    #[msg("A rent exemption constraint was violated")]
83    ConstraintRentExempt,
84    /// 2006 - A seeds constraint was violated
85    #[msg("A seeds constraint was violated")]
86    ConstraintSeeds,
87    /// 2007 - An executable constraint was violated
88    #[msg("An executable constraint was violated")]
89    ConstraintExecutable,
90    /// 2008 - Deprecated Error, feel free to replace with something else
91    #[msg("Deprecated Error, feel free to replace with something else")]
92    ConstraintState,
93    /// 2009 - An associated constraint was violated
94    #[msg("An associated constraint was violated")]
95    ConstraintAssociated,
96    /// 2010 - An associated init constraint was violated
97    #[msg("An associated init constraint was violated")]
98    ConstraintAssociatedInit,
99    /// 2011 - A close constraint was violated
100    #[msg("A close constraint was violated")]
101    ConstraintClose,
102    /// 2012 - An address constraint was violated
103    #[msg("An address constraint was violated")]
104    ConstraintAddress,
105    /// 2013 - Expected zero account discriminant
106    #[msg("Expected zero account discriminant")]
107    ConstraintZero,
108    /// 2014 - A token mint constraint was violated
109    #[msg("A token mint constraint was violated")]
110    ConstraintTokenMint,
111    /// 2015 - A token owner constraint was violated
112    #[msg("A token owner constraint was violated")]
113    ConstraintTokenOwner,
114    /// The mint mint is intentional -> a mint authority for the mint.
115    ///
116    /// 2016 - A mint mint authority constraint was violated
117    #[msg("A mint mint authority constraint was violated")]
118    ConstraintMintMintAuthority,
119    /// 2017 - A mint freeze authority constraint was violated
120    #[msg("A mint freeze authority constraint was violated")]
121    ConstraintMintFreezeAuthority,
122    /// 2018 - A mint decimals constraint was violated
123    #[msg("A mint decimals constraint was violated")]
124    ConstraintMintDecimals,
125    /// 2019 - A space constraint was violated
126    #[msg("A space constraint was violated")]
127    ConstraintSpace,
128    /// 2020 - A required account for the constraint is None
129    #[msg("A required account for the constraint is None")]
130    ConstraintAccountIsNone,
131    /// The token token is intentional -> a token program for the token account.
132    ///
133    /// 2021 - A token account token program constraint was violated
134    #[msg("A token account token program constraint was violated")]
135    ConstraintTokenTokenProgram,
136    /// 2022 - A mint token program constraint was violated
137    #[msg("A mint token program constraint was violated")]
138    ConstraintMintTokenProgram,
139    /// 2023 - A mint token program constraint was violated
140    #[msg("An associated token account token program constraint was violated")]
141    ConstraintAssociatedTokenTokenProgram,
142    /// Extension constraints
143    ///
144    /// 2024 - A group pointer extension constraint was violated
145    #[msg("A group pointer extension constraint was violated")]
146    ConstraintMintGroupPointerExtension,
147    /// 2025 - A group pointer extension authority constraint was violated
148    #[msg("A group pointer extension authority constraint was violated")]
149    ConstraintMintGroupPointerExtensionAuthority,
150    /// 2026 - A group pointer extension group address constraint was violated
151    #[msg("A group pointer extension group address constraint was violated")]
152    ConstraintMintGroupPointerExtensionGroupAddress,
153    /// 2027 - A group member pointer extension constraint was violated
154    #[msg("A group member pointer extension constraint was violated")]
155    ConstraintMintGroupMemberPointerExtension,
156    /// 2028 - A group member pointer extension authority constraint was violated
157    #[msg("A group member pointer extension authority constraint was violated")]
158    ConstraintMintGroupMemberPointerExtensionAuthority,
159    /// 2029 - A group member pointer extension member address constraint was violated
160    #[msg("A group member pointer extension group address constraint was violated")]
161    ConstraintMintGroupMemberPointerExtensionMemberAddress,
162    /// 2030 - A metadata pointer extension constraint was violated
163    #[msg("A metadata pointer extension constraint was violated")]
164    ConstraintMintMetadataPointerExtension,
165    /// 2031 - A metadata pointer extension authority constraint was violated
166    #[msg("A metadata pointer extension authority constraint was violated")]
167    ConstraintMintMetadataPointerExtensionAuthority,
168    /// 2032 - A metadata pointer extension metadata address constraint was violated
169    #[msg("A metadata pointer extension metadata address constraint was violated")]
170    ConstraintMintMetadataPointerExtensionMetadataAddress,
171    /// 2033 - A close authority extension constraint was violated
172    #[msg("A close authority constraint was violated")]
173    ConstraintMintCloseAuthorityExtension,
174    /// 2034 - A close authority extension authority constraint was violated
175    #[msg("A close authority extension authority constraint was violated")]
176    ConstraintMintCloseAuthorityExtensionAuthority,
177    /// 2035 - A permanent delegate extension constraint was violated
178    #[msg("A permanent delegate extension constraint was violated")]
179    ConstraintMintPermanentDelegateExtension,
180    /// 2036 - A permanent delegate extension authority constraint was violated
181    #[msg("A permanent delegate extension delegate constraint was violated")]
182    ConstraintMintPermanentDelegateExtensionDelegate,
183    /// 2037 - A transfer hook extension constraint was violated
184    #[msg("A transfer hook extension constraint was violated")]
185    ConstraintMintTransferHookExtension,
186    /// 2038 - A transfer hook extension authority constraint was violated
187    #[msg("A transfer hook extension authority constraint was violated")]
188    ConstraintMintTransferHookExtensionAuthority,
189    /// 2039 - A transfer hook extension transfer hook program id constraint was violated
190    #[msg("A transfer hook extension transfer hook program id constraint was violated")]
191    ConstraintMintTransferHookExtensionProgramId,
192    /// 2040 - A duplicate mutable account constraint was violated
193    #[msg("A duplicate mutable account constraint was violated")]
194    ConstraintDuplicateMutableAccount,
195
196    // Migration errors
197    /// 2041 - Account is already migrated
198    #[msg("Account is already migrated")]
199    AccountAlreadyMigrated,
200    /// 2042 - Account must be migrated before exiting
201    #[msg("Account must be migrated before exiting")]
202    AccountNotMigrated,
203
204    // Require
205    /// 2500 - A require expression was violated
206    #[msg("A require expression was violated")]
207    RequireViolated = 2500,
208    /// 2501 - A require_eq expression was violated
209    #[msg("A require_eq expression was violated")]
210    RequireEqViolated,
211    /// 2502 - A require_keys_eq expression was violated
212    #[msg("A require_keys_eq expression was violated")]
213    RequireKeysEqViolated,
214    /// 2503 - A require_neq expression was violated
215    #[msg("A require_neq expression was violated")]
216    RequireNeqViolated,
217    /// 2504 - A require_keys_neq expression was violated
218    #[msg("A require_keys_neq expression was violated")]
219    RequireKeysNeqViolated,
220    /// 2505 - A require_gt expression was violated
221    #[msg("A require_gt expression was violated")]
222    RequireGtViolated,
223    /// 2506 - A require_gte expression was violated
224    #[msg("A require_gte expression was violated")]
225    RequireGteViolated,
226
227    // Accounts.
228    /// 3000 - The account discriminator was already set on this account
229    #[msg("The account discriminator was already set on this account")]
230    AccountDiscriminatorAlreadySet = 3000,
231    /// 3001 - No discriminator was found on the account
232    #[msg("No discriminator was found on the account")]
233    AccountDiscriminatorNotFound,
234    /// 3002 - Account discriminator did not match what was expected
235    #[msg("Account discriminator did not match what was expected")]
236    AccountDiscriminatorMismatch,
237    /// 3003 - Failed to deserialize the account
238    #[msg("Failed to deserialize the account")]
239    AccountDidNotDeserialize,
240    /// 3004 - Failed to serialize the account
241    #[msg("Failed to serialize the account")]
242    AccountDidNotSerialize,
243    /// 3005 - Not enough account keys given to the instruction
244    #[msg("Not enough account keys given to the instruction")]
245    AccountNotEnoughKeys,
246    /// 3006 - The given account is not mutable
247    #[msg("The given account is not mutable")]
248    AccountNotMutable,
249    /// 3007 - The given account is owned by a different program than expected
250    #[msg("The given account is owned by a different program than expected")]
251    AccountOwnedByWrongProgram,
252    /// 3008 - Program ID was not as expected
253    #[msg("Program ID was not as expected")]
254    InvalidProgramId,
255    /// 3009 - Program account is not executable
256    #[msg("Program account is not executable")]
257    InvalidProgramExecutable,
258    /// 3010 - The given account did not sign
259    #[msg("The given account did not sign")]
260    AccountNotSigner,
261    /// 3011 - The given account is not owned by the system program
262    #[msg("The given account is not owned by the system program")]
263    AccountNotSystemOwned,
264    /// 3012 - The program expected this account to be already initialized
265    #[msg("The program expected this account to be already initialized")]
266    AccountNotInitialized,
267    /// 3013 - The given account is not a program data account
268    #[msg("The given account is not a program data account")]
269    AccountNotProgramData,
270    /// 3014 - The given account is not the associated token account
271    #[msg("The given account is not the associated token account")]
272    AccountNotAssociatedTokenAccount,
273    /// 3015 - The given public key does not match the required sysvar
274    #[msg("The given public key does not match the required sysvar")]
275    AccountSysvarMismatch,
276    /// 3016 - The account reallocation exceeds the MAX_PERMITTED_DATA_INCREASE limit
277    #[msg("The account reallocation exceeds the MAX_PERMITTED_DATA_INCREASE limit")]
278    AccountReallocExceedsLimit,
279    /// 3017 - The account was duplicated for more than one reallocation
280    #[msg("The account was duplicated for more than one reallocation")]
281    AccountDuplicateReallocs,
282
283    // Miscellaneous
284    /// 4100 - The declared program id does not match actual program id
285    #[msg("The declared program id does not match the actual program id")]
286    DeclaredProgramIdMismatch = 4100,
287    /// 4101 - You cannot/should not initialize the payer account as a program account
288    #[msg("You cannot/should not initialize the payer account as a program account")]
289    TryingToInitPayerAsProgramAccount = 4101,
290    /// 4102 - Invalid numeric conversion error
291    #[msg("Error during numeric conversion")]
292    InvalidNumericConversion = 4102,
293
294    // Deprecated
295    /// 5000 - The API being used is deprecated and should no longer be used
296    #[msg("The API being used is deprecated and should no longer be used")]
297    Deprecated = 5000,
298}
299
300#[derive(Debug, PartialEq, Eq)]
301pub enum Error {
302    AnchorError(Box<AnchorError>),
303    ProgramError(Box<ProgramErrorWithOrigin>),
304}
305
306impl std::error::Error for Error {}
307
308impl Display for Error {
309    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
310        match self {
311            Error::AnchorError(ae) => Display::fmt(&ae, f),
312            Error::ProgramError(pe) => Display::fmt(&pe, f),
313        }
314    }
315}
316
317impl From<AnchorError> for Error {
318    fn from(ae: AnchorError) -> Self {
319        Self::AnchorError(Box::new(ae))
320    }
321}
322
323impl From<ProgramError> for Error {
324    fn from(program_error: ProgramError) -> Self {
325        Self::ProgramError(Box::new(program_error.into()))
326    }
327}
328
329#[cfg(feature = "borsh")]
330impl From<borsh::io::Error> for Error {
331    fn from(error: borsh::io::Error) -> Self {
332        Error::ProgramError(Box::new(ProgramError::from(error).into()))
333    }
334}
335
336impl From<ProgramErrorWithOrigin> for Error {
337    fn from(pe: ProgramErrorWithOrigin) -> Self {
338        Self::ProgramError(Box::new(pe))
339    }
340}
341
342impl From<TryFromIntError> for Error {
343    fn from(e: TryFromIntError) -> Self {
344        Self::AnchorError(Box::new(AnchorError {
345            error_name: ErrorCode::InvalidNumericConversion.name(),
346            error_code_number: ErrorCode::InvalidNumericConversion.into(),
347            error_msg: format!("{e}"),
348            error_origin: None,
349            compared_values: None,
350        }))
351    }
352}
353
354impl From<std::convert::Infallible> for Error {
355    fn from(_: std::convert::Infallible) -> Self {
356        unreachable!("Infallible has no inhabitants")
357    }
358}
359
360impl Error {
361    pub fn log(&self) {
362        match self {
363            Error::ProgramError(program_error) => program_error.log(),
364            Error::AnchorError(anchor_error) => anchor_error.log(),
365        }
366    }
367
368    pub fn with_account_name(mut self, account_name: impl ToString) -> Self {
369        match &mut self {
370            Error::AnchorError(ae) => {
371                ae.error_origin = Some(ErrorOrigin::AccountName(account_name.to_string()));
372            }
373            Error::ProgramError(pe) => {
374                pe.error_origin = Some(ErrorOrigin::AccountName(account_name.to_string()));
375            }
376        };
377        self
378    }
379
380    pub fn with_source(mut self, source: Source) -> Self {
381        match &mut self {
382            Error::AnchorError(ae) => {
383                ae.error_origin = Some(ErrorOrigin::Source(source));
384            }
385            Error::ProgramError(pe) => {
386                pe.error_origin = Some(ErrorOrigin::Source(source));
387            }
388        };
389        self
390    }
391
392    pub fn with_pubkeys(mut self, pubkeys: (Pubkey, Pubkey)) -> Self {
393        let pubkeys = Some(ComparedValues::Pubkeys((pubkeys.0, pubkeys.1)));
394        match &mut self {
395            Error::AnchorError(ae) => ae.compared_values = pubkeys,
396            Error::ProgramError(pe) => pe.compared_values = pubkeys,
397        };
398        self
399    }
400
401    pub fn with_values(mut self, values: (impl ToString, impl ToString)) -> Self {
402        match &mut self {
403            Error::AnchorError(ae) => {
404                ae.compared_values = Some(ComparedValues::Values((
405                    values.0.to_string(),
406                    values.1.to_string(),
407                )))
408            }
409            Error::ProgramError(pe) => {
410                pe.compared_values = Some(ComparedValues::Values((
411                    values.0.to_string(),
412                    values.1.to_string(),
413                )))
414            }
415        };
416        self
417    }
418}
419
420#[derive(Debug)]
421pub struct ProgramErrorWithOrigin {
422    pub program_error: ProgramError,
423    pub error_origin: Option<ErrorOrigin>,
424    pub compared_values: Option<ComparedValues>,
425}
426
427// Two ProgramErrors are equal when they have the same error code
428impl PartialEq for ProgramErrorWithOrigin {
429    fn eq(&self, other: &Self) -> bool {
430        self.program_error == other.program_error
431    }
432}
433impl Eq for ProgramErrorWithOrigin {}
434
435impl Display for ProgramErrorWithOrigin {
436    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
437        Display::fmt(&self.program_error, f)
438    }
439}
440
441impl ProgramErrorWithOrigin {
442    pub fn log(&self) {
443        match &self.error_origin {
444            None => {
445                solana_msg::msg!(
446                    "ProgramError occurred. Error Code: {:?}. Error Number: {}. Error Message: {}.",
447                    self.program_error,
448                    u64::from(self.program_error.clone()),
449                    self.program_error
450                );
451            }
452            Some(ErrorOrigin::Source(source)) => {
453                solana_msg::msg!(
454                    "ProgramError thrown in {}:{}. Error Code: {:?}. Error Number: {}. Error \
455                     Message: {}.",
456                    source.filename,
457                    source.line,
458                    self.program_error,
459                    u64::from(self.program_error.clone()),
460                    self.program_error
461                );
462            }
463            Some(ErrorOrigin::AccountName(account_name)) => {
464                // using sol_log because msg! wrongly interprets 5 inputs as u64
465                solana_msg::msg!(&format!(
466                    "ProgramError caused by account: {}. Error Code: {:?}. Error Number: {}. \
467                     Error Message: {}.",
468                    account_name,
469                    self.program_error,
470                    u64::from(self.program_error.clone()),
471                    self.program_error
472                ));
473            }
474        }
475        match &self.compared_values {
476            Some(ComparedValues::Pubkeys((left, right))) => {
477                solana_msg::msg!("Left:");
478                left.log();
479                solana_msg::msg!("Right:");
480                right.log();
481            }
482            Some(ComparedValues::Values((left, right))) => {
483                solana_msg::msg!("Left: {}", left);
484                solana_msg::msg!("Right: {}", right);
485            }
486            None => (),
487        }
488    }
489
490    pub fn with_source(mut self, source: Source) -> Self {
491        self.error_origin = Some(ErrorOrigin::Source(source));
492        self
493    }
494
495    pub fn with_account_name(mut self, account_name: impl ToString) -> Self {
496        self.error_origin = Some(ErrorOrigin::AccountName(account_name.to_string()));
497        self
498    }
499}
500
501impl From<ProgramError> for ProgramErrorWithOrigin {
502    fn from(program_error: ProgramError) -> Self {
503        Self {
504            program_error,
505            error_origin: None,
506            compared_values: None,
507        }
508    }
509}
510
511#[derive(Debug)]
512pub enum ComparedValues {
513    Values((String, String)),
514    Pubkeys((Pubkey, Pubkey)),
515}
516
517#[derive(Debug)]
518pub enum ErrorOrigin {
519    Source(Source),
520    AccountName(String),
521}
522
523#[derive(Debug)]
524pub struct AnchorError {
525    pub error_name: String,
526    pub error_code_number: u32,
527    pub error_msg: String,
528    pub error_origin: Option<ErrorOrigin>,
529    pub compared_values: Option<ComparedValues>,
530}
531
532impl AnchorError {
533    pub fn log(&self) {
534        match &self.error_origin {
535            None => {
536                solana_msg::msg!(
537                    "AnchorError occurred. Error Code: {}. Error Number: {}. Error Message: {}.",
538                    self.error_name,
539                    self.error_code_number,
540                    self.error_msg
541                );
542            }
543            Some(ErrorOrigin::Source(source)) => {
544                solana_msg::msg!(
545                    "AnchorError thrown in {}:{}. Error Code: {}. Error Number: {}. Error \
546                     Message: {}.",
547                    source.filename,
548                    source.line,
549                    self.error_name,
550                    self.error_code_number,
551                    self.error_msg
552                );
553            }
554            Some(ErrorOrigin::AccountName(account_name)) => {
555                solana_msg::msg!(
556                    "AnchorError caused by account: {}. Error Code: {}. Error Number: {}. Error \
557                     Message: {}.",
558                    account_name,
559                    self.error_name,
560                    self.error_code_number,
561                    self.error_msg
562                );
563            }
564        }
565        match &self.compared_values {
566            Some(ComparedValues::Pubkeys((left, right))) => {
567                solana_msg::msg!("Left:");
568                left.log();
569                solana_msg::msg!("Right:");
570                right.log();
571            }
572            Some(ComparedValues::Values((left, right))) => {
573                solana_msg::msg!("Left: {}", left);
574                solana_msg::msg!("Right: {}", right);
575            }
576            None => (),
577        }
578    }
579
580    pub fn with_source(mut self, source: Source) -> Self {
581        self.error_origin = Some(ErrorOrigin::Source(source));
582        self
583    }
584
585    pub fn with_account_name(mut self, account_name: impl ToString) -> Self {
586        self.error_origin = Some(ErrorOrigin::AccountName(account_name.to_string()));
587        self
588    }
589}
590
591impl Display for AnchorError {
592    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
593        Debug::fmt(&self, f)
594    }
595}
596
597/// Two `AnchorError`s are equal when they have the same error code
598impl PartialEq for AnchorError {
599    fn eq(&self, other: &Self) -> bool {
600        self.error_code_number == other.error_code_number
601    }
602}
603
604impl Eq for AnchorError {}
605
606impl std::convert::From<Error> for ProgramError {
607    fn from(e: Error) -> ProgramError {
608        match e {
609            Error::AnchorError(error) => ProgramError::Custom(error.error_code_number),
610            Error::ProgramError(program_error) => program_error.program_error,
611        }
612    }
613}
614
615#[derive(Debug)]
616pub struct Source {
617    pub filename: &'static str,
618    pub line: u32,
619}