Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
56 changes: 56 additions & 0 deletions program-error/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,17 @@ impl fmt::Display for ProgramError {
}
}

#[deprecated(
since = "2.2.2",
note = "Use `ToStr` instead with `solana_msg::msg!` or any other logging"
)]
pub trait PrintProgramError {
fn print<E>(&self)
where
E: 'static + std::error::Error + DecodeError<E> + PrintProgramError + FromPrimitive;
}

#[allow(deprecated)]
impl PrintProgramError for ProgramError {
fn print<E>(&self)
where
Expand Down Expand Up @@ -176,6 +181,57 @@ impl PrintProgramError for ProgramError {
}
}

/// A trait for converting a program error to a `&str`.
pub trait ToStr {
fn to_str<E>(&self) -> &'static str
where
E: 'static + ToStr + TryFrom<u32>;
}

impl ToStr for ProgramError {
fn to_str<E>(&self) -> &'static str
where
E: 'static + ToStr + TryFrom<u32>,
{
match self {
Self::Custom(error) => {
if let Ok(custom_error) = E::try_from(*error) {
custom_error.to_str::<E>()
} else {
"Error: Unknown"
}
}
Self::InvalidArgument => "Error: InvalidArgument",
Self::InvalidInstructionData => "Error: InvalidInstructionData",
Self::InvalidAccountData => "Error: InvalidAccountData",
Self::AccountDataTooSmall => "Error: AccountDataTooSmall",
Self::InsufficientFunds => "Error: InsufficientFunds",
Self::IncorrectProgramId => "Error: IncorrectProgramId",
Self::MissingRequiredSignature => "Error: MissingRequiredSignature",
Self::AccountAlreadyInitialized => "Error: AccountAlreadyInitialized",
Self::UninitializedAccount => "Error: UninitializedAccount",
Self::NotEnoughAccountKeys => "Error: NotEnoughAccountKeys",
Self::AccountBorrowFailed => "Error: AccountBorrowFailed",
Self::MaxSeedLengthExceeded => "Error: MaxSeedLengthExceeded",
Self::InvalidSeeds => "Error: InvalidSeeds",
Self::BorshIoError(_) => "Error: BorshIoError",
Self::AccountNotRentExempt => "Error: AccountNotRentExempt",
Self::UnsupportedSysvar => "Error: UnsupportedSysvar",
Self::IllegalOwner => "Error: IllegalOwner",
Self::MaxAccountsDataAllocationsExceeded => "Error: MaxAccountsDataAllocationsExceeded",
Self::InvalidRealloc => "Error: InvalidRealloc",
Self::MaxInstructionTraceLengthExceeded => "Error: MaxInstructionTraceLengthExceeded",
Self::BuiltinProgramsMustConsumeComputeUnits => {
"Error: BuiltinProgramsMustConsumeComputeUnits"
}
Self::InvalidAccountOwner => "Error: InvalidAccountOwner",
Self::ArithmeticOverflow => "Error: ArithmeticOverflow",
Self::Immutable => "Error: Immutable",
Self::IncorrectAuthority => "Error: IncorrectAuthority",
}
}
}

impl From<ProgramError> for u64 {
fn from(error: ProgramError) -> Self {
match error {
Expand Down
4 changes: 3 additions & 1 deletion program/src/program_error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[allow(deprecated)]
pub use solana_program_error::PrintProgramError;
pub use {
solana_instruction::error::{
ACCOUNT_ALREADY_INITIALIZED, ACCOUNT_BORROW_FAILED, ACCOUNT_DATA_TOO_SMALL,
Expand All @@ -10,5 +12,5 @@ pub use {
MISSING_REQUIRED_SIGNATURES, NOT_ENOUGH_ACCOUNT_KEYS, UNINITIALIZED_ACCOUNT,
UNSUPPORTED_SYSVAR,
},
solana_program_error::{PrintProgramError, ProgramError},
solana_program_error::ProgramError,
};