SPL Program Error

Macros for implementing error-based traits on enums.

#[derive(IntoProgramError)]

This derive macro automatically derives the trait From<Self> for solana_program::program_error::ProgramError.

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

Sample code:

```rust /// Example error

[derive(

Clone, Debug, 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(DecodeError)]

This derive macro automatically derives the trait solana_program::decode_error::DecodeError<T>.

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

Sample code:

```rust /// Example error

[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::program_error::PrintProgramError.

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

Sample code:

```rust /// Example error

[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, } ```

#[spl_program_error]

It can be cumbersome to ensure your program's defined errors - typically represented in an enum - implement the required traits and will print to the program's logs when they're invoked.

This procedureal macro will give you all of the required implementations out of the box:

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


Just annotate your enum...

```rust use solanaprogramerror_derive::*;

/// Example error

[solanaprogramerror]

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, } ```

...and get:

```rust /// Example error 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, }

[automatically_derived]

impl ::core::clone::Clone for ExampleError { #[inline] fn clone(&self) -> ExampleError { match self { ExampleError::MintHasNoMintAuthority => ExampleError::MintHasNoMintAuthority, ExampleError::IncorrectMintAuthority => ExampleError::IncorrectMintAuthority, } } }

[automatically_derived]

impl ::core::fmt::Debug for ExampleError { fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { ::core::fmt::Formatter::write_str( f, match self { ExampleError::MintHasNoMintAuthority => "MintHasNoMintAuthority", ExampleError::IncorrectMintAuthority => "IncorrectMintAuthority", }, ) } }

[automatically_derived]

impl ::core::marker::StructuralEq for ExampleError {}

[automatically_derived]

impl ::core::cmp::Eq for ExampleError { #[inline] #[doc(hidden)] #[nocoverage] fn assertreceiveristotal_eq(&self) -> () {} }

[allow(unused_qualifications)]

impl std::error::Error for ExampleError {}

[allow(unused_qualifications)]

impl std::fmt::Display for ExampleError { fn fmt(&self, _formatter: &mut std::fmt::Formatter) -> std::fmt::Result { #[allow(unusedvariables, deprecated, clippy::usedunderscorebinding)] match self { ExampleError::MintHasNoMintAuthority {} => { _formatter.writefmt(formatargs!("Mint has no mint authority")) } ExampleError::IncorrectMintAuthority {} => { _formatter .writefmt( formatargs!( "Incorrect mint authority has signed the instruction" ), ) } } } }

[allow(nonuppercaseglobals, unusedqualifications)]

const IMPLNUMFromPrimitiveFORExampleError: () = { #[allow(clippy::uselessattribute)] #[allow(rust2018idioms)] extern crate numtraits as _numtraits; impl numtraits::FromPrimitive for ExampleError { #[allow(trivialnumericcasts)] #[inline] fn fromi64(n: i64) -> Option { if n == ExampleError::MintHasNoMintAuthority as i64 { Some(ExampleError::MintHasNoMintAuthority) } else if n == ExampleError::IncorrectMintAuthority as i64 { Some(ExampleError::IncorrectMintAuthority) } else { None } } #[inline] fn fromu64(n: u64) -> Option { Self::from_i64(n as i64) } } };

[automatically_derived]

impl ::core::marker::StructuralPartialEq for ExampleError {}

[automatically_derived]

impl ::core::cmp::PartialEq for ExampleError { #[inline] fn eq(&self, other: &ExampleError) -> bool { let _selftag = ::core::intrinsics::discriminantvalue(self); let _arg1tag = ::core::intrinsics::discriminantvalue(other); _selftag == _arg1tag } } impl From for solanaprogram::programerror::ProgramError { fn from(e: ExampleError) -> Self { solanaprogram::programerror::ProgramError::Custom(e as u32) } } impl solanaprogram::decodeerror::DecodeError for ExampleError { fn typeof() -> &'static str { "ExampleError" } } impl solanaprogram::programerror::PrintProgramError for ExampleError { fn print(&self) where E: 'static + std::error::Error + solanaprogram::decodeerror::DecodeError + solanaprogram::programerror::PrintProgramError + numtraits::FromPrimitive, { match self { ExampleError::MintHasNoMintAuthority => { ::solanaprogram::log::sollog("Mint has no mint authority") } ExampleError::IncorrectMintAuthority => { ::solanaprogram::log::sollog( "Incorrect mint authority has signed the instruction", ) } } } } ```