Skip to main content

eth_valkyoth_protocol/transaction/encode/
error.rs

1use core::fmt;
2
3use eth_valkyoth_codec::{DecodeError, DecodeErrorCategory};
4
5/// Transaction envelope encoding failure.
6#[non_exhaustive]
7#[derive(Clone, Copy, Debug, Eq, PartialEq)]
8pub enum TransactionEncodeError {
9    /// The underlying bounded RLP codec rejected a field or output buffer.
10    Codec(DecodeError),
11}
12
13impl TransactionEncodeError {
14    /// Stable machine-readable error code.
15    #[must_use]
16    pub const fn code(self) -> &'static str {
17        match self {
18            Self::Codec(error) => error.code(),
19        }
20    }
21
22    /// Stable human-readable error message.
23    #[must_use]
24    pub const fn message(self) -> &'static str {
25        match self {
26            Self::Codec(error) => error.message(),
27        }
28    }
29
30    /// Stable high-level category for policy decisions.
31    #[must_use]
32    pub const fn category(self) -> TransactionEncodeErrorCategory {
33        match self {
34            Self::Codec(error) => match error.category() {
35                DecodeErrorCategory::MalformedInput => {
36                    TransactionEncodeErrorCategory::MalformedInput
37                }
38                DecodeErrorCategory::ResourceExhaustion => {
39                    TransactionEncodeErrorCategory::ResourceExhaustion
40                }
41                _ => TransactionEncodeErrorCategory::MalformedInput,
42            },
43        }
44    }
45}
46
47impl From<DecodeError> for TransactionEncodeError {
48    fn from(error: DecodeError) -> Self {
49        Self::Codec(error)
50    }
51}
52
53impl fmt::Display for TransactionEncodeError {
54    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
55        formatter.write_str(self.message())
56    }
57}
58
59#[cfg(feature = "std")]
60impl std::error::Error for TransactionEncodeError {}
61
62/// Stable high-level transaction encode error categories.
63#[non_exhaustive]
64#[derive(Clone, Copy, Debug, Eq, PartialEq)]
65pub enum TransactionEncodeErrorCategory {
66    /// A field, length, or output buffer is malformed for canonical encoding.
67    MalformedInput,
68    /// A bounded codec policy rejected an encoded list payload.
69    ResourceExhaustion,
70}