Skip to main content

crabka_protocol/records/
error.rs

1//! Errors specific to record-batch decoding/encoding.
2
3use thiserror::Error;
4
5#[derive(Debug, Error)]
6#[non_exhaustive]
7pub enum RecordsError {
8    #[error("buffer too short for batch header (need {needed} more bytes)")]
9    HeaderTooShort { needed: usize },
10
11    #[error("batch magic byte {found} unsupported (only v2 supported)")]
12    UnsupportedMagic { found: i8 },
13
14    #[error("CRC mismatch: expected {expected:#010x}, computed {computed:#010x}")]
15    CrcMismatch { expected: u32, computed: u32 },
16
17    #[error("batch body truncated (need {needed} more bytes)")]
18    BodyTooShort { needed: usize },
19
20    #[error("record parse failed: {0}")]
21    RecordParse(String),
22
23    #[error("compression: {0}")]
24    Compression(#[from] crabka_compression::CompressionError),
25
26    #[error("zerocopy reinterpretation failed")]
27    ZerocopyFailure,
28}
29
30impl From<RecordsError> for crate::ProtocolError {
31    fn from(e: RecordsError) -> Self {
32        crate::ProtocolError::InvalidValue(match e {
33            RecordsError::HeaderTooShort { .. } => "records: header too short",
34            RecordsError::UnsupportedMagic { .. } => "records: unsupported magic",
35            RecordsError::CrcMismatch { .. } => "records: CRC mismatch",
36            RecordsError::BodyTooShort { .. } => "records: body truncated",
37            RecordsError::RecordParse(_) => "records: record parse failed",
38            RecordsError::Compression(_) => "records: compression error",
39            RecordsError::ZerocopyFailure => "records: zerocopy failure",
40        })
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn display_messages() {
50        let cases: &[(RecordsError, &str)] = &[
51            (
52                RecordsError::HeaderTooShort { needed: 4 },
53                "buffer too short for batch header",
54            ),
55            (
56                RecordsError::UnsupportedMagic { found: 1 },
57                "batch magic byte 1 unsupported",
58            ),
59            (
60                RecordsError::CrcMismatch {
61                    expected: 0xDEAD_BEEF,
62                    computed: 0x1234_5678,
63                },
64                "CRC mismatch: expected 0xdeadbeef",
65            ),
66            (
67                RecordsError::BodyTooShort { needed: 17 },
68                "batch body truncated",
69            ),
70            (
71                RecordsError::RecordParse("bad varint".into()),
72                "record parse failed",
73            ),
74            (
75                RecordsError::ZerocopyFailure,
76                "zerocopy reinterpretation failed",
77            ),
78        ];
79        for (err, contains) in cases {
80            assert!(
81                err.to_string().contains(contains),
82                "{err} did not contain {contains:?}",
83            );
84        }
85    }
86
87    #[test]
88    fn into_protocol_error_is_invalid_value() {
89        let e: crate::ProtocolError = RecordsError::UnsupportedMagic { found: 0 }.into();
90        assert!(matches!(e, crate::ProtocolError::InvalidValue(_)));
91    }
92}