Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement TryFromIntError conversion for AnchorError #2950

Merged
merged 8 commits into from
May 15, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lang/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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};
use std::num::TryFromIntError;

/// The starting point for user defined error codes.
pub const ERROR_CODE_OFFSET: u32 = 6000;
Expand Down Expand Up @@ -263,6 +264,9 @@ pub enum ErrorCode {
/// 4101 - You cannot/should not initialize the payer account as a program account
#[msg("You cannot/should not initialize the payer account as a program account")]
TryingToInitPayerAsProgramAccount = 4101,
/// 4102 - Invalid numeric conversion error
#[msg("Error during numeric conversion")]
InvalidNumericConversion = 4102,

// Deprecated
/// 5000 - The API being used is deprecated and should no longer be used
Expand Down Expand Up @@ -310,6 +314,18 @@ impl From<ProgramErrorWithOrigin> for Error {
}
}

impl From<TryFromIntError> for Error {
fn from(e: TryFromIntError) -> Self {
Self::AnchorError(Box::new(AnchorError {
error_name: ErrorCode::InvalidNumericConversion.name(),
error_code_number: ErrorCode::InvalidNumericConversion.into(),
error_msg: format!("{}", e),
error_origin: None,
compared_values: None,
}))
}
}

impl Error {
pub fn log(&self) {
match self {
Expand Down
Loading