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