error.rs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. //! Error types
  2. use solana_program_error::{ProgramError, ToStr};
  3. /// Errors that may be returned by the interface.
  4. #[repr(u32)]
  5. #[derive(Clone, Debug, Eq, thiserror::Error, num_derive::FromPrimitive, PartialEq)]
  6. pub enum TransferHookError {
  7. /// Incorrect account provided
  8. #[error("Incorrect account provided")]
  9. IncorrectAccount = 2_110_272_652,
  10. /// Mint has no mint authority
  11. #[error("Mint has no mint authority")]
  12. MintHasNoMintAuthority,
  13. /// Incorrect mint authority has signed the instruction
  14. #[error("Incorrect mint authority has signed the instruction")]
  15. IncorrectMintAuthority,
  16. /// Program called outside of a token transfer
  17. #[error("Program called outside of a token transfer")]
  18. ProgramCalledOutsideOfTransfer,
  19. }
  20. impl From<TransferHookError> for ProgramError {
  21. fn from(e: TransferHookError) -> Self {
  22. ProgramError::Custom(e as u32)
  23. }
  24. }
  25. impl ToStr for TransferHookError {
  26. fn to_str(&self) -> &'static str {
  27. match self {
  28. TransferHookError::IncorrectAccount => "Incorrect account provided",
  29. TransferHookError::MintHasNoMintAuthority => "Mint has no mint authority",
  30. TransferHookError::IncorrectMintAuthority => {
  31. "Incorrect mint authority has signed the instruction"
  32. }
  33. TransferHookError::ProgramCalledOutsideOfTransfer => {
  34. "Program called outside of a token transfer"
  35. }
  36. }
  37. }
  38. }
  39. impl TryFrom<u32> for TransferHookError {
  40. type Error = ProgramError;
  41. fn try_from(code: u32) -> Result<Self, Self::Error> {
  42. num_traits::FromPrimitive::from_u32(code).ok_or(ProgramError::InvalidArgument)
  43. }
  44. }