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