Skip to main content

odradek_protocol/
error.rs

1//! Error types shared by the wire codecs.
2
3/// A value could not be encoded into the Kafka wire format.
4#[derive(Debug, thiserror::Error, PartialEq, Eq)]
5#[non_exhaustive]
6pub enum EncodeError {
7    /// A string or byte sequence exceeds the maximum length its wire
8    /// representation can carry (e.g. a `STRING` longer than `i16::MAX`).
9    #[error("value of length {len} exceeds the wire format limit of {max}")]
10    TooLong { len: usize, max: usize },
11    /// A field was `None` but the message version being encoded does not
12    /// permit null for it.
13    #[error("field {0} may not be null in this message version")]
14    NullField(&'static str),
15}
16
17/// Bytes on the wire could not be decoded.
18#[derive(Debug, thiserror::Error, PartialEq, Eq)]
19#[non_exhaustive]
20pub enum DecodeError {
21    /// The buffer ended before the value was complete.
22    #[error("buffer truncated: needed {needed} more byte(s)")]
23    Truncated { needed: usize },
24    /// A varint ran past the maximum width for its type.
25    #[error("varint overflows a 64-bit value")]
26    VarintOverflow,
27    /// A length prefix was negative where null is not permitted, or does not
28    /// fit in memory on this platform.
29    #[error("invalid length prefix: {0}")]
30    InvalidLength(i64),
31    /// A `STRING` value was not valid UTF-8.
32    #[error("string is not valid UTF-8")]
33    InvalidUtf8,
34    /// An API key, error code, or other enum discriminant is unknown.
35    #[error("unknown {kind} discriminant: {value}")]
36    UnknownDiscriminant { kind: &'static str, value: i64 },
37    /// A record batch's stored checksum does not match its contents.
38    #[error("crc mismatch: batch stores {stored:#010x}, contents hash to {computed:#010x}")]
39    CrcMismatch { stored: u32, computed: u32 },
40    /// Decoding this input would allocate more than its
41    /// [`Budget`](crate::budget::Budget) allows — a count-prefixed array
42    /// asking for far more memory than the bytes that carry it.
43    #[error("decode would allocate {wanted} bytes, over this input's {limit}-byte budget")]
44    AllocationLimit { limit: usize, wanted: usize },
45    /// The allocator refused a request the budget had approved.
46    #[error("allocator refused {bytes} bytes while decoding (budget {limit})")]
47    AllocationFailed { bytes: usize, limit: usize },
48    /// A count-driven loop's element consumed no input. Legal counts
49    /// cannot outrun the bytes that back them; continuing would spin.
50    #[error("array element consumed no input")]
51    NoProgress,
52    /// A tagged-field section was not strictly ascending by tag. The
53    /// spec requires ascending order, and a repeated tag would let one
54    /// occurrence silently overwrite another.
55    #[error("tagged field {tag} does not follow {previous} in ascending order")]
56    TaggedFieldOrder { previous: u32, tag: u32 },
57}