1use core::num::TryFromIntError;
21use std::error::Error;
22use std::{cell, io, result, str};
23
24#[cfg(feature = "arrow")]
25use arrow_schema::ArrowError;
26
27#[derive(Debug)]
31#[non_exhaustive]
32pub enum ParquetError {
33 General(String),
36 NYI(String),
39 EOF(String),
43 #[cfg(feature = "arrow")]
44 ArrowError(String),
47 IndexOutOfBound(usize, usize),
50 External(Box<dyn Error + Send + Sync>),
52 NeedMoreData(usize),
55}
56
57impl std::fmt::Display for ParquetError {
58 fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
59 match &self {
60 ParquetError::General(message) => {
61 write!(fmt, "Parquet error: {message}")
62 }
63 ParquetError::NYI(message) => write!(fmt, "NYI: {message}"),
64 ParquetError::EOF(message) => write!(fmt, "EOF: {message}"),
65 #[cfg(feature = "arrow")]
66 ParquetError::ArrowError(message) => write!(fmt, "Arrow: {message}"),
67 ParquetError::IndexOutOfBound(index, ref bound) => {
68 write!(fmt, "Index {index} out of bound: {bound}")
69 }
70 ParquetError::External(e) => write!(fmt, "External: {e}"),
71 ParquetError::NeedMoreData(needed) => write!(fmt, "NeedMoreData: {needed}"),
72 }
73 }
74}
75
76impl Error for ParquetError {
77 fn source(&self) -> Option<&(dyn Error + 'static)> {
78 match self {
79 ParquetError::External(e) => Some(e.as_ref()),
80 _ => None,
81 }
82 }
83}
84
85impl From<TryFromIntError> for ParquetError {
86 fn from(e: TryFromIntError) -> ParquetError {
87 ParquetError::General(format!("Integer overflow: {e}"))
88 }
89}
90
91impl From<io::Error> for ParquetError {
92 fn from(e: io::Error) -> ParquetError {
93 ParquetError::External(Box::new(e))
94 }
95}
96
97#[cfg(any(feature = "snap", test))]
98impl From<snap::Error> for ParquetError {
99 fn from(e: snap::Error) -> ParquetError {
100 ParquetError::External(Box::new(e))
101 }
102}
103
104impl From<thrift::Error> for ParquetError {
105 fn from(e: thrift::Error) -> ParquetError {
106 ParquetError::External(Box::new(e))
107 }
108}
109
110impl From<cell::BorrowMutError> for ParquetError {
111 fn from(e: cell::BorrowMutError) -> ParquetError {
112 ParquetError::External(Box::new(e))
113 }
114}
115
116impl From<str::Utf8Error> for ParquetError {
117 fn from(e: str::Utf8Error) -> ParquetError {
118 ParquetError::External(Box::new(e))
119 }
120}
121#[cfg(feature = "arrow")]
122impl From<ArrowError> for ParquetError {
123 fn from(e: ArrowError) -> ParquetError {
124 ParquetError::External(Box::new(e))
125 }
126}
127
128#[cfg(feature = "object_store")]
129impl From<object_store::Error> for ParquetError {
130 fn from(e: object_store::Error) -> ParquetError {
131 ParquetError::External(Box::new(e))
132 }
133}
134
135pub type Result<T, E = ParquetError> = result::Result<T, E>;
137
138impl From<ParquetError> for io::Error {
142 fn from(e: ParquetError) -> Self {
143 io::Error::new(io::ErrorKind::Other, e)
144 }
145}
146
147macro_rules! general_err {
151 ($fmt:expr) => (ParquetError::General($fmt.to_owned()));
152 ($fmt:expr, $($args:expr),*) => (ParquetError::General(format!($fmt, $($args),*)));
153 ($e:expr, $fmt:expr) => (ParquetError::General($fmt.to_owned(), $e));
154 ($e:ident, $fmt:expr, $($args:tt),*) => (
155 ParquetError::General(&format!($fmt, $($args),*), $e));
156}
157
158macro_rules! nyi_err {
159 ($fmt:expr) => (ParquetError::NYI($fmt.to_owned()));
160 ($fmt:expr, $($args:expr),*) => (ParquetError::NYI(format!($fmt, $($args),*)));
161}
162
163macro_rules! eof_err {
164 ($fmt:expr) => (ParquetError::EOF($fmt.to_owned()));
165 ($fmt:expr, $($args:expr),*) => (ParquetError::EOF(format!($fmt, $($args),*)));
166}
167
168#[cfg(feature = "arrow")]
169macro_rules! arrow_err {
170 ($fmt:expr) => (ParquetError::ArrowError($fmt.to_owned()));
171 ($fmt:expr, $($args:expr),*) => (ParquetError::ArrowError(format!($fmt, $($args),*)));
172 ($e:expr, $fmt:expr) => (ParquetError::ArrowError($fmt.to_owned(), $e));
173 ($e:ident, $fmt:expr, $($args:tt),*) => (
174 ParquetError::ArrowError(&format!($fmt, $($args),*), $e));
175}
176
177#[cfg(feature = "arrow")]
181impl From<ParquetError> for ArrowError {
182 fn from(p: ParquetError) -> Self {
183 Self::ParquetError(format!("{p}"))
184 }
185}