1use std::fmt;
25
26use chrono::NaiveDateTime;
27use fmt::{Debug, Display};
28
29use crate::util::TsUnit;
30use crate::{ParseSchemaError, SchemaResolutionError};
31
32#[derive(Clone, Debug, Eq, PartialEq)]
33pub enum DecodeError {
34 CodecUtf8Error,
35 MapKeyUtf8Error,
36 StringUtf8Error,
37 UuidUtf8Error,
38 UnrecognizedCodec(String),
39 BadSnappyChecksum {
40 expected: u32,
41 actual: u32,
42 },
43 ExpectedNonnegInteger(i64),
44 BadTimestamp {
45 unit: TsUnit,
46 value: i64,
47 },
48 BadBoolean(u8),
49 BadDate(i32),
50 BadUnionIndex {
57 index: usize,
58 len: usize,
59 },
60 MissingUnionIndex(usize),
61 BadEnumIndex {
62 index: usize,
63 len: usize,
64 },
65 MissingEnumIndex {
66 index: usize,
67 symbol: String,
68 },
69 WrongUnionIndex {
70 expected: usize,
71 actual: usize,
72 },
73 WrongHeaderMagic([u8; 4]),
74 MissingAvroDotSchema,
75 I32OutOfRange(i64),
76 IntConversionError,
77 IntDecodeOverflow,
78 BadJson {
79 category: serde_json::error::Category,
80 bytes: Vec<u8>,
85 },
86 BadUuid(uuid::Error),
87 MismatchedBlockHeader {
88 expected: [u8; 16],
89 actual: [u8; 16],
90 },
91 DateOutOfRange(i32),
92 TimestampOutOfRange(NaiveDateTime),
93 Custom(String),
94}
95
96impl DecodeError {
97 fn fmt_inner(&self, f: &mut fmt::Formatter) -> fmt::Result {
98 match self {
99 DecodeError::UnrecognizedCodec(codec) => write!(f, "Unrecognized codec: {}", codec),
100 DecodeError::BadSnappyChecksum { expected, actual } => write!(
101 f,
102 "Bad Snappy CRC32; expected {:x} but got {:x}",
103 expected, actual
104 ),
105 DecodeError::ExpectedNonnegInteger(i) => {
106 write!(f, "Expected non-negative integer, got {}", i)
107 }
108 DecodeError::BadTimestamp { unit, value } => {
109 write!(f, "Invalid timestamp {value} {unit}")
110 }
111 DecodeError::BadBoolean(byte) => write!(f, "Invalid boolean: {:x}", byte),
112 DecodeError::BadDate(since_epoch) => {
113 write!(f, "Invalid num days since epoch: {}", since_epoch)
114 }
115 DecodeError::BadUnionIndex { index, len } => {
116 write!(f, "Union index out of bounds: {} (len: {})", index, len)
117 }
118 DecodeError::MissingUnionIndex(index) => {
119 write!(f, "Union variant not found in reader schema: {}", index)
120 }
121 DecodeError::BadEnumIndex { index, len } => write!(
122 f,
123 "Enum symbol index out of bounds: {} (len: {})",
124 index, len
125 ),
126 DecodeError::MissingEnumIndex { index, symbol } => write!(
127 f,
128 "Enum symbol {} at index {} in writer schema not found in reader",
129 symbol, index
130 ),
131 DecodeError::WrongHeaderMagic(magic) => write!(f, "Wrong header magic: {:x?}", magic),
132 DecodeError::MissingAvroDotSchema => write!(
133 f,
134 "Symbol's value as variable is void: avro.schema missing from header"
135 ),
136 DecodeError::I32OutOfRange(i) => write!(f, "Expected i32, got: {}", i),
137 DecodeError::IntDecodeOverflow => write!(f, "Overflow when decoding integer value"),
138 DecodeError::WrongUnionIndex { expected, actual } => write!(
139 f,
140 "Reader expected variant at index {}, got {}",
141 expected, actual
142 ),
143 DecodeError::Custom(inner) => write!(f, "Error in decode client: {}", inner),
144 DecodeError::CodecUtf8Error => write!(f, "Codec was not valid UTF-8"),
145 DecodeError::MapKeyUtf8Error => write!(f, "Map key was not valid UTF-8"),
146 DecodeError::StringUtf8Error => write!(f, "String was not valid UTF-8"),
147 DecodeError::UuidUtf8Error => write!(f, "UUID was not valid UTF-8"),
148 DecodeError::IntConversionError => write!(f, "Integer conversion failed"),
149 DecodeError::BadJson { category, bytes } => {
150 write!(f, "Json decoding failed: {:?}", category)?;
151 write!(f, " (got {})", String::from_utf8_lossy(bytes))
152 }
153 DecodeError::BadUuid(inner) => write!(f, "UUID decoding failed: {}", inner),
154 DecodeError::MismatchedBlockHeader { expected, actual } => write!(
155 f,
156 "Block marker ({:x?}) does not match header marker ({:x?})",
157 actual, expected
158 ),
159 DecodeError::DateOutOfRange(inner) => write!(f, "Date out of range: {}", inner),
160 DecodeError::TimestampOutOfRange(inner) => {
161 write!(f, "Timestamp out of range: {}", inner)
162 }
163 }
164 }
165}
166
167impl Display for DecodeError {
168 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
169 write!(f, "Decoding error: ")?;
170 self.fmt_inner(f)
171 }
172}
173
174#[derive(Clone, Debug, Eq, PartialEq)]
175pub enum Error {
178 Decode(DecodeError),
179 ParseSchema(ParseSchemaError), ResolveSchema(SchemaResolutionError), IO(std::io::ErrorKind), Allocation { attempted: usize, allowed: usize },
183}
184
185impl From<std::io::Error> for Error {
186 fn from(e: std::io::Error) -> Self {
187 Self::IO(e.kind())
188 }
189}
190
191impl From<std::convert::Infallible> for Error {
192 fn from(_: std::convert::Infallible) -> Self {
193 unreachable!()
194 }
195}
196
197impl From<std::num::TryFromIntError> for Error {
198 fn from(_: std::num::TryFromIntError) -> Self {
199 Self::Decode(DecodeError::IntConversionError)
200 }
201}
202
203impl From<DecodeError> for Error {
204 fn from(inner: DecodeError) -> Self {
205 Self::Decode(inner)
206 }
207}
208
209impl From<ParseSchemaError> for Error {
210 fn from(inner: ParseSchemaError) -> Self {
211 Self::ParseSchema(inner)
212 }
213}
214
215impl From<SchemaResolutionError> for Error {
216 fn from(inner: SchemaResolutionError) -> Self {
217 Self::ResolveSchema(inner)
218 }
219}
220
221impl Display for Error {
222 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
223 match self {
224 Error::Decode(inner) => write!(f, "Decode error: {}", inner),
225 Error::ParseSchema(inner) => write!(f, "Schema parse error: {}", inner),
226 Error::IO(inner_kind) => write!(f, "IO error: {:?}", inner_kind),
227 Error::Allocation { attempted, allowed } => write!(
228 f,
229 "Allocation error: attempt to allocate {} bytes (maximum allowed: {})",
230 attempted, allowed
231 ),
232 Error::ResolveSchema(inner) => write!(f, "Schema resolution error: {}", inner),
233 }
234 }
235}
236
237impl std::error::Error for Error {}