kacrab_protocol/record/
error.rs1use crate::{compression::CompressionError, crc::CrcMismatch, primitives::PrimitiveError};
8
9#[derive(Debug, thiserror::Error)]
11#[error("record batch failed at base_offset {base_offset}")]
12#[non_exhaustive]
13pub struct RecordError {
14 pub base_offset: i64,
16 #[source]
18 pub kind: RecordErrorKind,
19}
20
21impl RecordError {
22 #[must_use]
24 pub const fn at_offset(base_offset: i64, kind: RecordErrorKind) -> Self {
25 Self { base_offset, kind }
26 }
27
28 #[must_use]
30 pub const fn unknown_offset(kind: RecordErrorKind) -> Self {
31 Self {
32 base_offset: -1,
33 kind,
34 }
35 }
36}
37
38impl From<RecordErrorKind> for RecordError {
39 fn from(kind: RecordErrorKind) -> Self {
40 Self::unknown_offset(kind)
41 }
42}
43
44impl From<PrimitiveError> for RecordError {
45 fn from(err: PrimitiveError) -> Self {
46 Self::unknown_offset(RecordErrorKind::Primitive(err))
47 }
48}
49
50impl From<CrcMismatch> for RecordError {
51 fn from(err: CrcMismatch) -> Self {
52 Self::unknown_offset(RecordErrorKind::Crc(err))
53 }
54}
55
56impl From<CompressionError> for RecordError {
57 fn from(err: CompressionError) -> Self {
58 Self::unknown_offset(RecordErrorKind::Compression(err))
59 }
60}
61
62#[derive(Debug, thiserror::Error)]
64#[non_exhaustive]
65pub enum RecordErrorKind {
66 #[error(transparent)]
68 Primitive(#[from] PrimitiveError),
69
70 #[error(transparent)]
72 Crc(#[from] CrcMismatch),
73
74 #[error(transparent)]
76 Compression(#[from] CompressionError),
77
78 #[error("unsupported magic byte {0}, expected 2")]
80 UnsupportedMagic(i8),
81
82 #[error("batch length {got} below minimum {min}")]
84 BatchTooSmall {
85 got: i32,
87 min: i32,
89 },
90
91 #[error("record count {got} exceeds maximum {max}")]
93 RecordCountTooLarge {
94 got: i32,
96 max: usize,
98 },
99
100 #[error("negative {field} length {length}")]
102 NegativeLength {
103 field: &'static str,
105 length: i32,
107 },
108
109 #[error("{field} length {got} exceeds remaining {remaining}")]
111 LengthOverflow {
112 field: &'static str,
114 got: usize,
116 remaining: usize,
118 },
119}