1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
use anchor_lang::error_code;
use borsh::maybestd::io::Error as BorshIoError;
use solana_program::{program_error::ProgramError, pubkey::Pubkey};
use std::fmt::{Debug, Display};

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

/// 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
/// - >= 4100 misc error codes
/// - = 5000 deprecated error code
///
/// The starting point for user-defined errors is defined
/// by the [ERROR_CODE_OFFSET](crate::error::ERROR_CODE_OFFSET).
#[error_code(offset = 0)]
pub enum ErrorCode {
    // 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
    /// 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
    /// 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.
    ///
    /// 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,

    // Require
    /// 2500 - A require expression was violated
    #[msg("A require expression was violated")]
    RequireViolated = 2500,
    /// 2501 - A require_eq expression was violated
    #[msg("A require_eq expression was violated")]
    RequireEqViolated,
    /// 2502 - A require_keys_eq expression was violated
    #[msg("A require_keys_eq expression was violated")]
    RequireKeysEqViolated,
    /// 2503 - A require_neq expression was violated
    #[msg("A require_neq expression was violated")]
    RequireNeqViolated,
    /// 2504 - A require_keys_neq expression was violated
    #[msg("A require_keys_neq expression was violated")]
    RequireKeysNeqViolated,
    /// 2505 - A require_gt expression was violated
    #[msg("A require_gt expression was violated")]
    RequireGtViolated,
    /// 2506 - A require_gte expression was violated
    #[msg("A require_gte expression was violated")]
    RequireGteViolated,

    // 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,
    /// 3014 - The given account is not the associated token account
    #[msg("The given account is not the associated token account")]
    AccountNotAssociatedTokenAccount,
    /// 3015 - The given public key does not match the required sysvar
    #[msg("The given public key does not match the required sysvar")]
    AccountSysvarMismatch,
    /// 3016 - The account reallocation exceeds the MAX_PERMITTED_DATA_INCREASE limit
    #[msg("The account reallocation exceeds the MAX_PERMITTED_DATA_INCREASE limit")]
    AccountReallocExceedsLimit,
    /// 3017 - The account was duplicated for more than one reallocation
    #[msg("The account was duplicated for more than one reallocation")]
    AccountDuplicateReallocs,

    // 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,

    // Miscellaneous
    /// 4100 - The declared program id does not match actual program id
    #[msg("The declared program id does not match the actual program id")]
    DeclaredProgramIdMismatch = 4100,

    // 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,
}

#[derive(Debug, PartialEq, Eq)]
pub enum Error {
    AnchorError(AnchorError),
    ProgramError(ProgramErrorWithOrigin),
}

impl std::error::Error for Error {}

impl Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::AnchorError(ae) => Display::fmt(&ae, f),
            Error::ProgramError(pe) => Display::fmt(&pe, f),
        }
    }
}

impl From<AnchorError> for Error {
    fn from(ae: AnchorError) -> Self {
        Self::AnchorError(ae)
    }
}

impl From<ProgramError> for Error {
    fn from(program_error: ProgramError) -> Self {
        Self::ProgramError(program_error.into())
    }
}
impl From<BorshIoError> for Error {
    fn from(error: BorshIoError) -> Self {
        Error::ProgramError(ProgramError::from(error).into())
    }
}

impl From<ProgramErrorWithOrigin> for Error {
    fn from(pe: ProgramErrorWithOrigin) -> Self {
        Self::ProgramError(pe)
    }
}

impl Error {
    pub fn log(&self) {
        match self {
            Error::ProgramError(program_error) => program_error.log(),
            Error::AnchorError(anchor_error) => anchor_error.log(),
        }
    }

    pub fn with_account_name(mut self, account_name: impl ToString) -> Self {
        match &mut self {
            Error::AnchorError(ae) => {
                ae.error_origin = Some(ErrorOrigin::AccountName(account_name.to_string()));
            }
            Error::ProgramError(pe) => {
                pe.error_origin = Some(ErrorOrigin::AccountName(account_name.to_string()));
            }
        };
        self
    }

    pub fn with_source(mut self, source: Source) -> Self {
        match &mut self {
            Error::AnchorError(ae) => {
                ae.error_origin = Some(ErrorOrigin::Source(source));
            }
            Error::ProgramError(pe) => {
                pe.error_origin = Some(ErrorOrigin::Source(source));
            }
        };
        self
    }

    pub fn with_pubkeys(mut self, pubkeys: (Pubkey, Pubkey)) -> Self {
        let pubkeys = Some(ComparedValues::Pubkeys((pubkeys.0, pubkeys.1)));
        match &mut self {
            Error::AnchorError(ae) => ae.compared_values = pubkeys,
            Error::ProgramError(pe) => pe.compared_values = pubkeys,
        };
        self
    }

    pub fn with_values(mut self, values: (impl ToString, impl ToString)) -> Self {
        match &mut self {
            Error::AnchorError(ae) => {
                ae.compared_values = Some(ComparedValues::Values((
                    values.0.to_string(),
                    values.1.to_string(),
                )))
            }
            Error::ProgramError(pe) => {
                pe.compared_values = Some(ComparedValues::Values((
                    values.0.to_string(),
                    values.1.to_string(),
                )))
            }
        };
        self
    }
}

#[derive(Debug)]
pub struct ProgramErrorWithOrigin {
    pub program_error: ProgramError,
    pub error_origin: Option<ErrorOrigin>,
    pub compared_values: Option<ComparedValues>,
}

// Two ProgramErrors are equal when they have the same error code
impl PartialEq for ProgramErrorWithOrigin {
    fn eq(&self, other: &Self) -> bool {
        self.program_error == other.program_error
    }
}
impl Eq for ProgramErrorWithOrigin {}

impl Display for ProgramErrorWithOrigin {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Display::fmt(&self.program_error, f)
    }
}

impl ProgramErrorWithOrigin {
    pub fn log(&self) {
        match &self.error_origin {
            None => {
                anchor_lang::solana_program::msg!(
                    "ProgramError occurred. Error Code: {:?}. Error Number: {}. Error Message: {}.",
                    self.program_error,
                    u64::from(self.program_error.clone()),
                    self.program_error
                );
            }
            Some(ErrorOrigin::Source(source)) => {
                anchor_lang::solana_program::msg!(
                    "ProgramError thrown in {}:{}. Error Code: {:?}. Error Number: {}. Error Message: {}.",
                    source.filename,
                    source.line,
                    self.program_error,
                    u64::from(self.program_error.clone()),
                    self.program_error
                );
            }
            Some(ErrorOrigin::AccountName(account_name)) => {
                // using sol_log because msg! wrongly interprets 5 inputs as u64
                anchor_lang::solana_program::log::sol_log(&format!(
                    "ProgramError caused by account: {}. Error Code: {:?}. Error Number: {}. Error Message: {}.",
                    account_name,
                    self.program_error,
                    u64::from(self.program_error.clone()),
                    self.program_error
                ));
            }
        }
        match &self.compared_values {
            Some(ComparedValues::Pubkeys((left, right))) => {
                anchor_lang::solana_program::msg!("Left:");
                left.log();
                anchor_lang::solana_program::msg!("Right:");
                right.log();
            }
            Some(ComparedValues::Values((left, right))) => {
                anchor_lang::solana_program::msg!("Left: {}", left);
                anchor_lang::solana_program::msg!("Right: {}", right);
            }
            None => (),
        }
    }

    pub fn with_source(mut self, source: Source) -> Self {
        self.error_origin = Some(ErrorOrigin::Source(source));
        self
    }

    pub fn with_account_name(mut self, account_name: impl ToString) -> Self {
        self.error_origin = Some(ErrorOrigin::AccountName(account_name.to_string()));
        self
    }
}

impl From<ProgramError> for ProgramErrorWithOrigin {
    fn from(program_error: ProgramError) -> Self {
        Self {
            program_error,
            error_origin: None,
            compared_values: None,
        }
    }
}

#[derive(Debug)]
pub enum ComparedValues {
    Values((String, String)),
    Pubkeys((Pubkey, Pubkey)),
}

#[derive(Debug)]
pub enum ErrorOrigin {
    Source(Source),
    AccountName(String),
}

#[derive(Debug)]
pub struct AnchorError {
    pub error_name: String,
    pub error_code_number: u32,
    pub error_msg: String,
    pub error_origin: Option<ErrorOrigin>,
    pub compared_values: Option<ComparedValues>,
}

impl AnchorError {
    pub fn log(&self) {
        match &self.error_origin {
            None => {
                anchor_lang::solana_program::log::sol_log(&format!(
                    "AnchorError occurred. Error Code: {}. Error Number: {}. Error Message: {}.",
                    self.error_name, self.error_code_number, self.error_msg
                ));
            }
            Some(ErrorOrigin::Source(source)) => {
                anchor_lang::solana_program::msg!(
                    "AnchorError thrown in {}:{}. Error Code: {}. Error Number: {}. Error Message: {}.",
                    source.filename,
                    source.line,
                    self.error_name,
                    self.error_code_number,
                    self.error_msg
                );
            }
            Some(ErrorOrigin::AccountName(account_name)) => {
                anchor_lang::solana_program::log::sol_log(&format!(
                    "AnchorError caused by account: {}. Error Code: {}. Error Number: {}. Error Message: {}.",
                    account_name,
                    self.error_name,
                    self.error_code_number,
                    self.error_msg
                ));
            }
        }
        match &self.compared_values {
            Some(ComparedValues::Pubkeys((left, right))) => {
                anchor_lang::solana_program::msg!("Left:");
                left.log();
                anchor_lang::solana_program::msg!("Right:");
                right.log();
            }
            Some(ComparedValues::Values((left, right))) => {
                anchor_lang::solana_program::msg!("Left: {}", left);
                anchor_lang::solana_program::msg!("Right: {}", right);
            }
            None => (),
        }
    }

    pub fn with_source(mut self, source: Source) -> Self {
        self.error_origin = Some(ErrorOrigin::Source(source));
        self
    }

    pub fn with_account_name(mut self, account_name: impl ToString) -> Self {
        self.error_origin = Some(ErrorOrigin::AccountName(account_name.to_string()));
        self
    }
}

impl Display for AnchorError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Debug::fmt(&self, f)
    }
}

/// Two `AnchorError`s are equal when they have the same error code
impl PartialEq for AnchorError {
    fn eq(&self, other: &Self) -> bool {
        self.error_code_number == other.error_code_number
    }
}

impl Eq for AnchorError {}

impl std::convert::From<Error> for anchor_lang::solana_program::program_error::ProgramError {
    fn from(e: Error) -> anchor_lang::solana_program::program_error::ProgramError {
        match e {
            Error::AnchorError(AnchorError {
                error_code_number, ..
            }) => {
                anchor_lang::solana_program::program_error::ProgramError::Custom(error_code_number)
            }
            Error::ProgramError(program_error) => program_error.program_error,
        }
    }
}

#[derive(Debug)]
pub struct Source {
    pub filename: &'static str,
    pub line: u32,
}