use spl_program_error::*;
#[spl_program_error]
pub enum ExampleError {
#[error("Mint has no mint authority")]
MintHasNoMintAuthority,
#[error("Incorrect mint authority has signed the instruction")]
IncorrectMintAuthority,
}
#[test]
fn test_macros_compile() {
let _ = ExampleError::MintHasNoMintAuthority;
}
#[spl_program_error(hash_error_code_start = 2_056_342_880)]
enum ExampleLibraryError {
#[error("This is a very informative error")]
VeryInformativeError,
#[error("This is a super important error")]
SuperImportantError,
#[error("This is a mega serious error")]
MegaSeriousError,
#[error("You are toast")]
YouAreToast,
}
#[test]
fn test_library_error_codes() {
fn get_error_code_check(hash_input: &str) -> u32 {
let mut nonce: u32 = 0;
loop {
let hash = solana_sha256_hasher::hashv(&[hash_input.as_bytes(), &nonce.to_le_bytes()]);
let mut bytes = [0u8; 4];
bytes.copy_from_slice(&hash.to_bytes()[13..17]);
let error_code = u32::from_le_bytes(bytes);
if error_code >= 10_000 {
return error_code;
}
nonce += 1;
}
}
let first_error_as_u32 = ExampleLibraryError::VeryInformativeError as u32;
assert_eq!(
ExampleLibraryError::VeryInformativeError as u32,
get_error_code_check("spl_program_error:ExampleLibraryError"),
);
assert_eq!(
ExampleLibraryError::SuperImportantError as u32,
first_error_as_u32 + 1,
);
assert_eq!(
ExampleLibraryError::MegaSeriousError as u32,
first_error_as_u32 + 2,
);
assert_eq!(
ExampleLibraryError::YouAreToast as u32,
first_error_as_u32 + 3,
);
}
#[spl_program_error(solana_program_error = "solana_program_error")]
enum ExampleSolanaProgramError {
#[error("This is a very informative error")]
VeryInformativeError,
#[error("This is a super important error")]
SuperImportantError,
}
#[test]
fn test_macros_compile_with_solana_program_error_crate() {
let _ = ExampleSolanaProgramError::VeryInformativeError;
}