1use arrow_schema::ArrowError;
8use core::num::TryFromIntError;
9use std::error::Error;
10use std::string::FromUtf8Error;
11use std::{io, str};
12
13#[derive(Debug)]
16#[non_exhaustive]
17pub enum AvroError {
18 General(String),
21 NYI(String),
24 EOF(String),
28 ArrowError(Box<ArrowError>),
31 IndexOutOfBound(usize, usize),
34 InvalidArgument(String),
36 ParseError(String),
38 SchemaError(String),
40 External(Box<dyn Error + Send + Sync>),
42 IoError(String, io::Error),
44 NeedMoreData(usize),
47 NeedMoreDataRange(std::ops::Range<u64>),
50 BatchFull,
52}
53
54impl std::fmt::Display for AvroError {
55 fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
56 match &self {
57 AvroError::General(message) => {
58 write!(fmt, "Avro error: {message}")
59 }
60 AvroError::NYI(message) => write!(fmt, "NYI: {message}"),
61 AvroError::EOF(message) => write!(fmt, "EOF: {message}"),
62 AvroError::ArrowError(message) => write!(fmt, "Arrow: {message}"),
63 AvroError::IndexOutOfBound(index, bound) => {
64 write!(fmt, "Index {index} out of bound: {bound}")
65 }
66 AvroError::InvalidArgument(message) => {
67 write!(fmt, "Invalid argument: {message}")
68 }
69 AvroError::ParseError(message) => write!(fmt, "Parser error: {message}"),
70 AvroError::SchemaError(message) => write!(fmt, "Schema error: {message}"),
71 AvroError::External(e) => write!(fmt, "External: {e}"),
72 AvroError::IoError(message, e) => write!(fmt, "I/O Error: {message}: {e}"),
73 AvroError::NeedMoreData(needed) => write!(fmt, "NeedMoreData: {needed}"),
74 AvroError::NeedMoreDataRange(range) => {
75 write!(fmt, "NeedMoreDataRange: {}..{}", range.start, range.end)
76 }
77 AvroError::BatchFull => {
78 write!(fmt, "Batch is full; flush before decoding another datum")
79 }
80 }
81 }
82}
83
84impl Error for AvroError {
85 fn source(&self) -> Option<&(dyn Error + 'static)> {
86 match self {
87 AvroError::External(e) => Some(e.as_ref()),
88 AvroError::ArrowError(e) => Some(e.as_ref()),
89 AvroError::IoError(_, e) => Some(e),
90 _ => None,
91 }
92 }
93}
94
95impl From<TryFromIntError> for AvroError {
96 fn from(e: TryFromIntError) -> AvroError {
97 AvroError::General(format!("Integer overflow: {e}"))
98 }
99}
100
101impl From<io::Error> for AvroError {
102 fn from(e: io::Error) -> AvroError {
103 AvroError::External(Box::new(e))
104 }
105}
106
107impl From<str::Utf8Error> for AvroError {
108 fn from(e: str::Utf8Error) -> AvroError {
109 AvroError::External(Box::new(e))
110 }
111}
112
113impl From<FromUtf8Error> for AvroError {
114 fn from(e: FromUtf8Error) -> AvroError {
115 AvroError::External(Box::new(e))
116 }
117}
118
119impl From<ArrowError> for AvroError {
120 fn from(e: ArrowError) -> Self {
121 AvroError::ArrowError(Box::new(e))
122 }
123}
124
125#[cfg(feature = "object_store")]
126impl From<object_store::Error> for AvroError {
127 fn from(e: object_store::Error) -> AvroError {
128 AvroError::External(Box::new(e))
129 }
130}
131
132impl From<AvroError> for io::Error {
133 fn from(e: AvroError) -> Self {
134 io::Error::other(e)
135 }
136}
137
138impl From<AvroError> for ArrowError {
139 fn from(e: AvroError) -> Self {
140 match e {
141 AvroError::External(inner) => ArrowError::from_external_error(inner),
142 AvroError::IoError(msg, err) => ArrowError::IoError(msg, err),
143 AvroError::ArrowError(inner) => *inner,
144 other => ArrowError::AvroError(other.to_string()),
145 }
146 }
147}