apl_token_metadata/
error.rs

1//! Error types
2
3use {
4    arch_program::{
5        decode_error::DecodeError,
6        msg,
7        program_error::{PrintProgramError, ProgramError},
8    },
9    num_derive::FromPrimitive,
10    thiserror::Error,
11};
12
13/// Errors that may be returned by the Token Metadata program.
14#[derive(Clone, Debug, Eq, Error, FromPrimitive, PartialEq)]
15pub enum MetadataError {
16    // 0
17    /// Invalid mint
18    #[error("Invalid mint")]
19    InvalidMint,
20    /// Metadata already exists
21    #[error("Metadata already exists")]
22    MetadataAlreadyExists,
23    /// Metadata not found
24    #[error("Metadata not found")]
25    MetadataNotFound,
26    /// Invalid authority
27    #[error("Invalid authority")]
28    InvalidAuthority,
29    /// Invalid instruction data
30    #[error("Invalid instruction data")]
31    InvalidInstructionData,
32    /// String too long
33    #[error("String too long")]
34    StringTooLong,
35    /// Too many attributes
36    #[error("Too many attributes")]
37    TooManyAttributes,
38}
39
40impl From<MetadataError> for ProgramError {
41    fn from(e: MetadataError) -> Self {
42        ProgramError::Custom(e as u32)
43    }
44}
45
46impl<T> DecodeError<T> for MetadataError {
47    fn type_of() -> &'static str {
48        "MetadataError"
49    }
50}
51
52impl PrintProgramError for MetadataError {
53    fn print<E>(&self)
54    where
55        E: 'static
56            + std::error::Error
57            + DecodeError<E>
58            + PrintProgramError
59            + num_traits::FromPrimitive,
60    {
61        match self {
62            MetadataError::InvalidMint => msg!("Error: Invalid mint"),
63            MetadataError::MetadataAlreadyExists => msg!("Error: Metadata already exists"),
64            MetadataError::MetadataNotFound => msg!("Error: Metadata not found"),
65            MetadataError::InvalidAuthority => msg!("Error: Invalid authority"),
66            MetadataError::InvalidInstructionData => msg!("Error: Invalid instruction data"),
67            MetadataError::StringTooLong => msg!("Error: String too long"),
68            MetadataError::TooManyAttributes => msg!("Error: Too many attributes"),
69        }
70    }
71}