Skip to main content

kacrab_protocol/bytes_io/
error.rs

1//! Error types for [`crate::bytes_io`].
2
3use crate::primitives::PrimitiveError;
4
5/// Error returned by raw bytes read/write operations.
6#[derive(Debug, thiserror::Error)]
7#[error("Kafka bytes codec failed")]
8#[non_exhaustive]
9pub struct BytesError {
10    /// What specifically went wrong.
11    #[source]
12    pub kind: BytesErrorKind,
13}
14
15impl BytesError {
16    /// Construct a `BytesError` from its kind.
17    #[must_use]
18    pub const fn new(kind: BytesErrorKind) -> Self {
19        Self { kind }
20    }
21}
22
23impl From<BytesErrorKind> for BytesError {
24    fn from(kind: BytesErrorKind) -> Self {
25        Self::new(kind)
26    }
27}
28
29impl From<PrimitiveError> for BytesError {
30    fn from(err: PrimitiveError) -> Self {
31        Self::new(BytesErrorKind::Primitive(err))
32    }
33}
34
35/// Specific reason a bytes read/write failed.
36#[derive(Debug, thiserror::Error)]
37#[non_exhaustive]
38pub enum BytesErrorKind {
39    /// Underlying primitive read failed (length prefix).
40    #[error(transparent)]
41    Primitive(#[from] PrimitiveError),
42
43    /// Non-nullable variant got the null marker.
44    #[error("non-nullable bytes has null marker; use the nullable variant")]
45    UnexpectedNull,
46
47    /// Length prefix is negative on a non-nullable encoding.
48    #[error("negative length {length} on non-nullable bytes")]
49    NegativeLength {
50        /// The negative length read.
51        length: i32,
52    },
53
54    /// Bytes payload exceeds the maximum encodable length.
55    #[error("bytes length {length} exceeds maximum {max}")]
56    TooLong {
57        /// Offending length: the value being encoded, or `usize::MAX` as a
58        /// sentinel when a wire length prefix did not fit in `usize`.
59        length: usize,
60        /// Protocol-constant maximum (e.g. `i32::MAX`), not a tunable.
61        max: usize,
62    },
63}