Skip to content

Commit 1971070

Browse files
authored
program-error: Deprecate PrintProgramError (#102)
#### Problem As part of the breaking change to unite pinocchio's program error with the sdk's program error in #12, we are introducing a new `ToStr` trait that doesn't rely on any particular logging. To go with that, we want to deprecate `PrintProgramError`. #### Summary of changes Before landing the breaking change, let's deprecate `PrintProgramError` properly and add `ToStr` as an alternative for downstream users. Once this lands, we'll do the patch release with the deprecation, then we can finally land #12.
1 parent 94202fe commit 1971070

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

program-error/src/lib.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,17 @@ impl fmt::Display for ProgramError {
122122
}
123123
}
124124

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

135+
#[allow(deprecated)]
131136
impl PrintProgramError for ProgramError {
132137
fn print<E>(&self)
133138
where
@@ -176,6 +181,57 @@ impl PrintProgramError for ProgramError {
176181
}
177182
}
178183

184+
/// A trait for converting a program error to a `&str`.
185+
pub trait ToStr {
186+
fn to_str<E>(&self) -> &'static str
187+
where
188+
E: 'static + ToStr + TryFrom<u32>;
189+
}
190+
191+
impl ToStr for ProgramError {
192+
fn to_str<E>(&self) -> &'static str
193+
where
194+
E: 'static + ToStr + TryFrom<u32>,
195+
{
196+
match self {
197+
Self::Custom(error) => {
198+
if let Ok(custom_error) = E::try_from(*error) {
199+
custom_error.to_str::<E>()
200+
} else {
201+
"Error: Unknown"
202+
}
203+
}
204+
Self::InvalidArgument => "Error: InvalidArgument",
205+
Self::InvalidInstructionData => "Error: InvalidInstructionData",
206+
Self::InvalidAccountData => "Error: InvalidAccountData",
207+
Self::AccountDataTooSmall => "Error: AccountDataTooSmall",
208+
Self::InsufficientFunds => "Error: InsufficientFunds",
209+
Self::IncorrectProgramId => "Error: IncorrectProgramId",
210+
Self::MissingRequiredSignature => "Error: MissingRequiredSignature",
211+
Self::AccountAlreadyInitialized => "Error: AccountAlreadyInitialized",
212+
Self::UninitializedAccount => "Error: UninitializedAccount",
213+
Self::NotEnoughAccountKeys => "Error: NotEnoughAccountKeys",
214+
Self::AccountBorrowFailed => "Error: AccountBorrowFailed",
215+
Self::MaxSeedLengthExceeded => "Error: MaxSeedLengthExceeded",
216+
Self::InvalidSeeds => "Error: InvalidSeeds",
217+
Self::BorshIoError(_) => "Error: BorshIoError",
218+
Self::AccountNotRentExempt => "Error: AccountNotRentExempt",
219+
Self::UnsupportedSysvar => "Error: UnsupportedSysvar",
220+
Self::IllegalOwner => "Error: IllegalOwner",
221+
Self::MaxAccountsDataAllocationsExceeded => "Error: MaxAccountsDataAllocationsExceeded",
222+
Self::InvalidRealloc => "Error: InvalidRealloc",
223+
Self::MaxInstructionTraceLengthExceeded => "Error: MaxInstructionTraceLengthExceeded",
224+
Self::BuiltinProgramsMustConsumeComputeUnits => {
225+
"Error: BuiltinProgramsMustConsumeComputeUnits"
226+
}
227+
Self::InvalidAccountOwner => "Error: InvalidAccountOwner",
228+
Self::ArithmeticOverflow => "Error: ArithmeticOverflow",
229+
Self::Immutable => "Error: Immutable",
230+
Self::IncorrectAuthority => "Error: IncorrectAuthority",
231+
}
232+
}
233+
}
234+
179235
impl From<ProgramError> for u64 {
180236
fn from(error: ProgramError) -> Self {
181237
match error {

program/src/program_error.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#[allow(deprecated)]
2+
pub use solana_program_error::PrintProgramError;
13
pub use {
24
solana_instruction::error::{
35
ACCOUNT_ALREADY_INITIALIZED, ACCOUNT_BORROW_FAILED, ACCOUNT_DATA_TOO_SMALL,
@@ -10,5 +12,5 @@ pub use {
1012
MISSING_REQUIRED_SIGNATURES, NOT_ENOUGH_ACCOUNT_KEYS, UNINITIALIZED_ACCOUNT,
1113
UNSUPPORTED_SYSVAR,
1214
},
13-
solana_program_error::{PrintProgramError, ProgramError},
15+
solana_program_error::ProgramError,
1416
};

0 commit comments

Comments
 (0)