Skip to main content

solana_message/versions/v1/
error.rs

1use solana_sanitize::SanitizeError;
2
3/// Errors that can occur when working with V1 messages.
4#[derive(Debug, Clone, PartialEq, Eq)]
5#[non_exhaustive]
6pub enum MessageError {
7    /// Input buffer is too small during deserialization.
8    BufferTooSmall,
9    /// Instruction has too many accounts (> 255).
10    InstructionAccountsTooLarge,
11    /// Instruction data is too large (> 65535 bytes).
12    InstructionDataTooLarge,
13    /// Invalid TransactionConfigMask.
14    InvalidConfigMask,
15    /// Instruction account index is out of bounds.
16    InvalidInstructionAccountIndex,
17    /// Program ID index is invalid (out of bounds or fee payer).
18    InvalidProgramIdIndex,
19    /// Invalid or missing version byte (expected 0x81).
20    InvalidVersion,
21    /// Lifetime specifier (blockhash) is required.
22    MissingLifetimeSpecifier,
23    /// Not enough addresses for the number of required signatures.
24    NotEnoughAddressesForSignatures,
25    /// Too many addresses to encode in the wire format (> 255).
26    TooManyAddresses,
27    /// Too many instructions to encode in the wire format (> 255).
28    TooManyInstructions,
29    /// Unexpected trailing data after message.
30    TrailingData,
31    /// Transaction exceeds maximum size (4096 bytes).
32    TransactionTooLarge,
33    /// Must have at least one signer (fee payer).
34    ZeroSigners,
35    /// Invalid configuration value.
36    InvalidConfigValue,
37    /// Not enough account keys provided.
38    NotEnoughAccountKeys,
39}
40
41impl core::fmt::Display for MessageError {
42    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
43        match self {
44            Self::BufferTooSmall => write!(f, "buffer too small"),
45            Self::InstructionAccountsTooLarge => {
46                write!(f, "instruction has too many accounts (max 255)")
47            }
48            Self::InstructionDataTooLarge => {
49                write!(f, "instruction data too large (max 65535 bytes)")
50            }
51            Self::InvalidConfigMask => write!(f, "invalid transaction config mask"),
52            Self::InvalidInstructionAccountIndex => {
53                write!(f, "instruction account index out of bounds")
54            }
55            Self::InvalidProgramIdIndex => {
56                write!(f, "program ID index out of bounds or is fee payer")
57            }
58            Self::InvalidVersion => write!(f, "invalid version byte (expected 0x81)"),
59            Self::MissingLifetimeSpecifier => {
60                write!(f, "lifetime specifier (blockhash) is required")
61            }
62            Self::NotEnoughAddressesForSignatures => {
63                write!(f, "not enough addresses for required signatures")
64            }
65            Self::TooManyAddresses => write!(f, "too many addresses (max 255)"),
66            Self::TooManyInstructions => write!(f, "too many instructions (max 255)"),
67            Self::TrailingData => write!(f, "unexpected trailing data"),
68            Self::TransactionTooLarge => write!(f, "transaction exceeds max size (4096 bytes)"),
69            Self::ZeroSigners => write!(f, "must have at least one signer (fee payer)"),
70            Self::InvalidConfigValue => write!(f, "invalid configuration value"),
71            Self::NotEnoughAccountKeys => write!(f, "not enough account keys provided"),
72        }
73    }
74}
75
76impl core::error::Error for MessageError {}
77
78impl From<MessageError> for SanitizeError {
79    fn from(err: MessageError) -> Self {
80        match err {
81            MessageError::BufferTooSmall
82            | MessageError::InstructionAccountsTooLarge
83            | MessageError::InstructionDataTooLarge
84            | MessageError::InvalidConfigMask
85            | MessageError::InvalidVersion
86            | MessageError::MissingLifetimeSpecifier
87            | MessageError::TrailingData
88            | MessageError::TransactionTooLarge
89            | MessageError::ZeroSigners
90            | MessageError::InvalidConfigValue
91            | MessageError::NotEnoughAccountKeys => SanitizeError::InvalidValue,
92            MessageError::InvalidInstructionAccountIndex
93            | MessageError::InvalidProgramIdIndex
94            | MessageError::NotEnoughAddressesForSignatures
95            | MessageError::TooManyAddresses
96            | MessageError::TooManyInstructions => SanitizeError::IndexOutOfBounds,
97        }
98    }
99}