Skip to content

Commit c97e6c7

Browse files
committed
Add error logging
1 parent 8034006 commit c97e6c7

File tree

5 files changed

+96
-15
lines changed

5 files changed

+96
-15
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

interface/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ readme = "./README.md"
1212
crate-type = ["rlib"]
1313

1414
[dependencies]
15-
pinocchio = { version = "0.7", git = "https://github.com/febo/pinocchio.git", branch = "febo/close-unstable" }
16-
pinocchio-pubkey = { version = "0.2", git = "https://github.com/febo/pinocchio.git", branch = "febo/close-unstable" }
15+
pinocchio = { version = "0.7", git = "https://github.com/febo/pinocchio.git", branch = "febo/error-to-str" }
16+
pinocchio-pubkey = { version = "0.2", git = "https://github.com/febo/pinocchio.git", branch = "febo/error-to-str" }
1717

1818
[dev-dependencies]
1919
strum = "0.27"

interface/src/error.rs

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Error types
22
3-
use pinocchio::program_error::ProgramError;
3+
use pinocchio::program_error::{ProgramError, ToStr};
44

55
/// Errors that may be returned by the Token program.
66
#[derive(Clone, Debug, Eq, PartialEq)]
@@ -59,3 +59,72 @@ impl From<TokenError> for ProgramError {
5959
ProgramError::Custom(e as u32)
6060
}
6161
}
62+
63+
impl ToStr for TokenError {
64+
fn to_str<E>(&self) -> &'static str
65+
where
66+
E: 'static + ToStr + TryFrom<u32>,
67+
{
68+
match self {
69+
TokenError::NotRentExempt => "Error: Lamport balance below rent-exempt threshold",
70+
TokenError::InsufficientFunds => "Error: insufficient funds",
71+
TokenError::InvalidMint => "Error: Invalid Mint",
72+
TokenError::MintMismatch => "Error: Account not associated with this Mint",
73+
TokenError::OwnerMismatch => "Error: owner does not match",
74+
TokenError::FixedSupply => "Error: the total supply of this token is fixed",
75+
TokenError::AlreadyInUse => "Error: account or token already in use",
76+
TokenError::InvalidNumberOfProvidedSigners => {
77+
"Error: Invalid number of provided signers"
78+
}
79+
TokenError::InvalidNumberOfRequiredSigners => {
80+
"Error: Invalid number of required signers"
81+
}
82+
TokenError::UninitializedState => "Error: State is uninitialized",
83+
TokenError::NativeNotSupported => "Error: Instruction does not support native tokens",
84+
TokenError::NonNativeHasBalance => {
85+
"Error: Non-native account can only be closed if its balance is zero"
86+
}
87+
TokenError::InvalidInstruction => "Error: Invalid instruction",
88+
TokenError::InvalidState => "Error: Invalid account state for operation",
89+
TokenError::Overflow => "Error: Operation overflowed",
90+
TokenError::AuthorityTypeNotSupported => {
91+
"Error: Account does not support specified authority type"
92+
}
93+
TokenError::MintCannotFreeze => "Error: This token mint cannot freeze accounts",
94+
TokenError::AccountFrozen => "Error: Account is frozen",
95+
TokenError::MintDecimalsMismatch => "Error: decimals different from the Mint decimals",
96+
TokenError::NonNativeNotSupported => {
97+
"Error: Instruction does not support non-native tokens"
98+
}
99+
}
100+
}
101+
}
102+
103+
impl TryFrom<u32> for TokenError {
104+
type Error = ProgramError;
105+
fn try_from(value: u32) -> Result<Self, Self::Error> {
106+
match value {
107+
0 => Ok(TokenError::NotRentExempt),
108+
1 => Ok(TokenError::InsufficientFunds),
109+
2 => Ok(TokenError::InvalidMint),
110+
3 => Ok(TokenError::MintMismatch),
111+
4 => Ok(TokenError::OwnerMismatch),
112+
5 => Ok(TokenError::FixedSupply),
113+
6 => Ok(TokenError::AlreadyInUse),
114+
7 => Ok(TokenError::InvalidNumberOfProvidedSigners),
115+
8 => Ok(TokenError::InvalidNumberOfRequiredSigners),
116+
9 => Ok(TokenError::UninitializedState),
117+
10 => Ok(TokenError::NativeNotSupported),
118+
11 => Ok(TokenError::NonNativeHasBalance),
119+
12 => Ok(TokenError::InvalidInstruction),
120+
13 => Ok(TokenError::InvalidState),
121+
14 => Ok(TokenError::Overflow),
122+
15 => Ok(TokenError::AuthorityTypeNotSupported),
123+
16 => Ok(TokenError::MintCannotFreeze),
124+
17 => Ok(TokenError::AccountFrozen),
125+
18 => Ok(TokenError::MintDecimalsMismatch),
126+
19 => Ok(TokenError::NonNativeNotSupported),
127+
_ => Err(ProgramError::InvalidArgument),
128+
}
129+
}
130+
}

p-token/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ crate-type = ["cdylib"]
1515
logging = []
1616

1717
[dependencies]
18-
pinocchio = { version = "0.7", git = "https://github.com/febo/pinocchio.git", branch = "febo/close-unstable" }
19-
pinocchio-log = { version = "0.3", git = "https://github.com/febo/pinocchio.git", branch = "febo/close-unstable" }
18+
pinocchio = { version = "0.7", git = "https://github.com/febo/pinocchio.git", branch = "febo/error-to-str" }
19+
pinocchio-log = { version = "0.3", git = "https://github.com/febo/pinocchio.git", branch = "febo/error-to-str" }
2020
spl-token-interface = { version = "^0", path = "../interface" }
2121

2222
[dev-dependencies]

p-token/src/entrypoint.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
use pinocchio::{
2-
account_info::AccountInfo, default_panic_handler, no_allocator, program_entrypoint,
3-
program_error::ProgramError, pubkey::Pubkey, ProgramResult,
2+
account_info::AccountInfo,
3+
default_panic_handler, no_allocator, program_entrypoint,
4+
program_error::{ProgramError, ToStr},
5+
pubkey::Pubkey,
6+
ProgramResult,
47
};
8+
use spl_token_interface::error::TokenError;
59

610
use crate::processor::*;
711

@@ -11,6 +15,12 @@ no_allocator!();
1115
// Use the default panic handler.
1216
default_panic_handler!();
1317

18+
/// Log an error.
19+
#[cold]
20+
fn log_error(error: &ProgramError) {
21+
pinocchio::log::sol_log(error.to_str::<TokenError>());
22+
}
23+
1424
/// Process an instruction.
1525
///
1626
/// In the first stage, the entrypoint checks the discriminator of the instruction data
@@ -27,15 +37,17 @@ pub fn process_instruction(
2737
return Err(ProgramError::InvalidInstructionData);
2838
};
2939

30-
if *discriminator == 255 {
40+
let result = if *discriminator == 255 {
3141
// 255 - Batch
3242
#[cfg(feature = "logging")]
3343
pinocchio::msg!("Instruction: Batch");
3444

35-
return process_batch(accounts, remaining);
36-
}
45+
process_batch(accounts, remaining)
46+
} else {
47+
inner_process_instruction(accounts, instruction_data)
48+
};
3749

38-
inner_process_instruction(accounts, instruction_data)
50+
result.inspect_err(log_error)
3951
}
4052

4153
/// Process a "regular" instruction.

0 commit comments

Comments
 (0)