Skip to main content

kacrab_protocol/frame/
error.rs

1//! Error types for [`crate::frame`].
2
3use crate::primitives::PrimitiveError;
4
5/// Error from frame read/write.
6#[derive(Debug, thiserror::Error)]
7#[error("kafka frame codec failed")]
8#[non_exhaustive]
9pub struct FrameError {
10    /// What specifically went wrong.
11    #[source]
12    pub kind: FrameErrorKind,
13}
14
15impl FrameError {
16    /// Construct a `FrameError` from its kind.
17    #[must_use]
18    pub const fn new(kind: FrameErrorKind) -> Self {
19        Self { kind }
20    }
21}
22
23impl From<FrameErrorKind> for FrameError {
24    fn from(kind: FrameErrorKind) -> Self {
25        Self::new(kind)
26    }
27}
28
29impl From<PrimitiveError> for FrameError {
30    fn from(err: PrimitiveError) -> Self {
31        Self::new(FrameErrorKind::Primitive(err))
32    }
33}
34
35/// Specific reason a frame operation failed.
36#[derive(Debug, thiserror::Error)]
37#[non_exhaustive]
38pub enum FrameErrorKind {
39    /// Underlying primitive read failed (length prefix).
40    #[error(transparent)]
41    Primitive(#[from] PrimitiveError),
42
43    /// Length prefix is negative.
44    #[error("negative frame length: {length}")]
45    NegativeLength {
46        /// The negative length read.
47        length: i32,
48    },
49
50    /// Length prefix exceeds [`crate::frame::MAX_FRAME_LENGTH`].
51    #[error("frame length {length} exceeds maximum {max}")]
52    TooLarge {
53        /// Length read from the wire.
54        length: i32,
55        /// Configured maximum.
56        max: i32,
57    },
58
59    /// Buffer ran out before the declared payload was consumed.
60    #[error("frame truncated: needed {needed} bytes, only {available} available")]
61    Truncated {
62        /// Bytes the frame declared.
63        needed: usize,
64        /// Bytes actually remaining.
65        available: usize,
66    },
67}