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
11 changes: 6 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion discriminator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ borsh = ["dep:borsh"]
[dependencies]
borsh = { version = "1", optional = true, features = ["derive"] }
bytemuck = { version = "1.23.1", features = ["derive"] }
solana-program-error = "2.2.1"
solana-program-error = "2.2.2"
solana-sha256-hasher = "2.2.1"
spl-discriminator-derive = { version = "0.2.0", path = "./derive" }

Expand Down
5 changes: 2 additions & 3 deletions pod/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ borsh = { version = "1.5.7", features = ["derive", "unstable__schema"], optional
bytemuck = { version = "1.23.1" }
bytemuck_derive = { version = "1.9.3" }
num-derive = "0.4"
num_enum = "0.7"
num-traits = "0.2"
serde = { version = "1.0.219", optional = true }
solana-decode-error = "2.2.1"
solana-msg = "2.2.1"
solana-program-error = "2.2.1"
solana-program-error = "2.2.2"
solana-program-option = "2.2.1"
solana-pubkey = "2.2.1"
solana-zk-sdk = "2.2.0"
Expand Down
40 changes: 15 additions & 25 deletions pod/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
//! Error types
use {
solana_msg::msg,
solana_program_error::{PrintProgramError, ProgramError},
};
use solana_program_error::{ProgramError, ToStr};

/// Errors that may be returned by the spl-pod library.
#[repr(u32)]
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error, num_derive::FromPrimitive)]
#[derive(
Debug,
Clone,
PartialEq,
Eq,
thiserror::Error,
num_enum::TryFromPrimitive,
num_derive::FromPrimitive,
)]
pub enum PodSliceError {
/// Error in checked math operation
#[error("Error in checked math operation")]
Expand All @@ -25,27 +30,12 @@ impl From<PodSliceError> for ProgramError {
}
}

impl<T> solana_decode_error::DecodeError<T> for PodSliceError {
fn type_of() -> &'static str {
"PodSliceError"
}
}

impl PrintProgramError for PodSliceError {
fn print<E>(&self)
where
E: 'static + std::error::Error + PrintProgramError + num_traits::FromPrimitive,
{
impl ToStr for PodSliceError {
fn to_str<E>(&self) -> &'static str {
match self {
PodSliceError::CalculationFailure => {
msg!("Error in checked math operation")
}
PodSliceError::BufferTooSmall => {
msg!("Provided byte buffer too small for expected type")
}
PodSliceError::BufferTooLarge => {
msg!("Provided byte buffer too large for expected type")
}
PodSliceError::CalculationFailure => "Error in checked math operation",
PodSliceError::BufferTooSmall => "Provided byte buffer too small for expected type",
PodSliceError::BufferTooLarge => "Provided byte buffer too large for expected type",
}
}
}
4 changes: 1 addition & 3 deletions pod/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@ pub mod slice;

// Export current sdk types for downstream users building with a different sdk
// version
pub use {
solana_decode_error, solana_msg, solana_program_error, solana_program_option, solana_pubkey,
};
pub use {solana_program_error, solana_program_option, solana_pubkey};
3 changes: 2 additions & 1 deletion program-error/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ edition = "2021"

[dependencies]
num-derive = "0.4"
num_enum = "0.7"
num-traits = "0.2"
solana-decode-error = "2.2.1"
solana-msg = "2.2.1"
solana-program-error = "2.2.1"
solana-program-error = "2.2.2"
spl-program-error-derive = { version = "0.5.0", path = "./derive" }
thiserror = "2.0"

Expand Down
82 changes: 14 additions & 68 deletions program-error/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
Macros for implementing error-based traits on enums.

- `#[derive(IntoProgramError)]`: automatically derives the trait `From<Self> for solana_program_error::ProgramError`.
- `#[derive(DecodeError)]`: automatically derives the trait `solana_decode_error::DecodeError<T>`.
- `#[derive(PrintProgramError)]`: automatically derives the trait `solana_program_error::PrintProgramError`.
- `#[derive(ToStr)]`: automatically derives the trait `solana_program_error::ToStr`.
- `#[spl_program_error]`: Automatically derives all below traits:
- `Clone`
- `Debug`
- `Eq`
- `DecodeError`
- `IntoProgramError`
- `PrintProgramError`
- `ToStr`
- `thiserror::Error`
- `num_derive::FromPrimitive`
- `num_enum::TryFromPrimitive`
- `PartialEq`

### `#[derive(IntoProgramError)]`
Expand All @@ -27,6 +26,7 @@ Your enum must implement the following traits in order for this macro to work:
- `Eq`
- `thiserror::Error`
- `num_derive::FromPrimitive`
- `num_enum::TryFromPrimitive`
- `PartialEq`

Sample code:
Expand All @@ -46,18 +46,17 @@ pub enum ExampleError {
}
```

### `#[derive(DecodeError)]`
### `#[derive(ToStr)]`

This derive macro automatically derives the trait `solana_decode_error::DecodeError<T>`.
This derive macro automatically derives the trait `solana_program_error::ToStr`.

Your enum must implement the following traits in order for this macro to work:

- `Clone`
- `Debug`
- `Eq`
- `IntoProgramError` (above)
- `thiserror::Error`
- `num_derive::FromPrimitive`
- `num_enum::TryFromPrimitive`
- `PartialEq`

Sample code:
Expand All @@ -67,50 +66,11 @@ Sample code:
#[derive(
Clone,
Debug,
DecodeError,
Eq,
IntoProgramError,
thiserror::Error,
num_derive::FromPrimitive,
PartialEq,
)]
pub enum ExampleError {
/// Mint has no mint authority
#[error("Mint has no mint authority")]
MintHasNoMintAuthority,
/// Incorrect mint authority has signed the instruction
#[error("Incorrect mint authority has signed the instruction")]
IncorrectMintAuthority,
}
```

### `#[derive(PrintProgramError)]`

This derive macro automatically derives the trait `solana_program_error::PrintProgramError`.

Your enum must implement the following traits in order for this macro to work:

- `Clone`
- `Debug`
- `DecodeError<T>` (above)
- `Eq`
- `IntoProgramError` (above)
- `thiserror::Error`
- `num_derive::FromPrimitive`
- `PartialEq`

Sample code:

```rust
/// Example error
#[derive(
Clone,
Debug,
DecodeError,
Eq,
IntoProgramError,
thiserror::Error,
num_derive::FromPrimitive,
num_enum::TryFromPrimitive,
PartialEq,
)]
pub enum ExampleError {
Expand All @@ -136,11 +96,13 @@ This procedural macro will give you all of the required implementations out of t
- `Eq`
- `thiserror::Error`
- `num_derive::FromPrimitive`
- `num_enum::TryFromPrimitive`
- `PartialEq`

It also imports the required crates so you don't have to in your program:

- `num_derive`
- `num_enum`
- `num_traits`
- `thiserror`

Expand Down Expand Up @@ -266,27 +228,11 @@ impl From<ExampleError> for solana_program_error::ProgramError {
solana_program_error::ProgramError::Custom(e as u32)
}
}
impl<T> solana_decode_error::DecodeError<T> for ExampleError {
fn type_of() -> &'static str {
"ExampleError"
}
}
impl solana_program_error::PrintProgramError for ExampleError {
fn print<E>(&self)
where
E: 'static + std::error::Error + solana_decode_error::DecodeError<E>
+ solana_program_error::PrintProgramError
+ num_traits::FromPrimitive,
{
impl solana_program_error::ToStr for ExampleError {
fn to_str<E>(&self) -> &'static str {
match self {
ExampleError::MintHasNoMintAuthority => {
::solana_msg::sol_log("Mint has no mint authority")
}
ExampleError::IncorrectMintAuthority => {
::solana_msg::sol_log(
"Incorrect mint authority has signed the instruction",
)
}
ExampleError::MintHasNoMintAuthority => "Mint has no mint authority",
ExampleError::IncorrectMintAuthority => "Incorrect mint authority has signed the instruction",
}
}
}
Expand Down
20 changes: 6 additions & 14 deletions program-error/derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// Required to include `#[allow(clippy::integer_arithmetic)]`
// below since the tokens generated by `quote!` in the implementation
// for `MacroType::PrintProgramError` and `MacroType::SplProgramError`
// for `MacroType::ToStr` and `MacroType::SplProgramError`
// trigger the lint upstream through `quote_token_with_context` within the
// `quote` crate
//
Expand Down Expand Up @@ -33,20 +33,13 @@ pub fn into_program_error(input: TokenStream) -> TokenStream {
.into()
}

/// Derive macro to add `solana_decode_error::DecodeError` trait
#[proc_macro_derive(DecodeError)]
pub fn decode_error(input: TokenStream) -> TokenStream {
let ItemEnum { ident, .. } = parse_macro_input!(input as ItemEnum);
MacroType::DecodeError { ident }.generate_tokens().into()
}

/// Derive macro to add `solana_program_error::PrintProgramError` trait
#[proc_macro_derive(PrintProgramError)]
pub fn print_program_error(input: TokenStream) -> TokenStream {
/// Derive macro to add `solana_program_error::ToStr` trait
#[proc_macro_derive(ToStr)]
pub fn to_str(input: TokenStream) -> TokenStream {
let ItemEnum {
ident, variants, ..
} = parse_macro_input!(input as ItemEnum);
MacroType::PrintProgramError { ident, variants }
MacroType::ToStr { ident, variants }
.generate_tokens()
.into()
}
Expand All @@ -61,8 +54,7 @@ pub fn print_program_error(input: TokenStream) -> TokenStream {
/// - `thiserror::Error`
/// - `num_derive::FromPrimitive`
/// - `Into<solana_program_error::ProgramError>`
/// - `solana_decode_error::DecodeError`
/// - `solana_program_error::PrintProgramError`
/// - `solana_program_error::ToStr`
///
/// Optionally, you can add `hash_error_code_start: u32` argument to create
/// a unique `u32` _starting_ error codes from the names of the enum variants.
Expand Down
Loading