use crate::{compression::CompressionError, crc::CrcMismatch, primitives::PrimitiveError};
#[derive(Debug, thiserror::Error)]
#[error("record batch failed at base_offset {base_offset}")]
#[non_exhaustive]
pub struct RecordError {
pub base_offset: i64,
#[source]
pub kind: RecordErrorKind,
}
impl RecordError {
#[must_use]
pub const fn at_offset(base_offset: i64, kind: RecordErrorKind) -> Self {
Self { base_offset, kind }
}
#[must_use]
pub const fn unknown_offset(kind: RecordErrorKind) -> Self {
Self {
base_offset: -1,
kind,
}
}
}
impl From<RecordErrorKind> for RecordError {
fn from(kind: RecordErrorKind) -> Self {
Self::unknown_offset(kind)
}
}
impl From<PrimitiveError> for RecordError {
fn from(err: PrimitiveError) -> Self {
Self::unknown_offset(RecordErrorKind::Primitive(err))
}
}
impl From<CrcMismatch> for RecordError {
fn from(err: CrcMismatch) -> Self {
Self::unknown_offset(RecordErrorKind::Crc(err))
}
}
impl From<CompressionError> for RecordError {
fn from(err: CompressionError) -> Self {
Self::unknown_offset(RecordErrorKind::Compression(err))
}
}
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum RecordErrorKind {
#[error(transparent)]
Primitive(#[from] PrimitiveError),
#[error(transparent)]
Crc(#[from] CrcMismatch),
#[error(transparent)]
Compression(#[from] CompressionError),
#[error("unsupported magic byte {0}, expected 2")]
UnsupportedMagic(i8),
#[error("batch length {got} below minimum {min}")]
BatchTooSmall {
got: i32,
min: i32,
},
#[error("record count {got} exceeds maximum {max}")]
RecordCountTooLarge {
got: i32,
max: usize,
},
#[error("negative {field} length {length}")]
NegativeLength {
field: &'static str,
length: i32,
},
#[error("{field} length {got} exceeds remaining {remaining}")]
LengthOverflow {
field: &'static str,
got: usize,
remaining: usize,
},
}