-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy patherror.rs
237 lines (217 loc) · 7.1 KB
/
error.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
use crate::types::idx_or_name::IdxOrName;
use calamine::XlsxError;
use std::{error::Error, fmt::Display};
#[derive(Debug)]
pub(crate) enum FastExcelErrorKind {
UnsupportedColumnTypeCombination(String),
CannotRetrieveCellData(usize, usize),
CalamineCellError(calamine::CellErrorType),
CalamineError(calamine::Error),
SheetNotFound(IdxOrName),
ColumnNotFound(IdxOrName),
// Arrow errors can be of several different types (arrow::error::Error, PyError), and having
// the actual type has not much value for us, so we just store a string context
ArrowError(String),
InvalidParameters(String),
Internal(String),
}
impl Display for FastExcelErrorKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
FastExcelErrorKind::UnsupportedColumnTypeCombination(detail) => {
write!(f, "unsupported column type combination: {detail}")
}
FastExcelErrorKind::CannotRetrieveCellData(row, col) => {
write!(f, "cannot retrieve cell data at ({row}, {col})")
}
FastExcelErrorKind::CalamineCellError(calamine_error) => {
write!(f, "calamine cell error: {calamine_error}")
}
FastExcelErrorKind::CalamineError(calamine_error) => {
write!(f, "calamine error: {calamine_error}")
}
FastExcelErrorKind::SheetNotFound(idx_or_name) => {
let message = idx_or_name.format_message();
write!(f, "sheet {message} not found")
}
FastExcelErrorKind::ColumnNotFound(idx_or_name) => {
let message = idx_or_name.format_message();
write!(f, "column {message} not found")
}
FastExcelErrorKind::ArrowError(err) => write!(f, "arrow error: {err}"),
FastExcelErrorKind::InvalidParameters(err) => write!(f, "invalid parameters: {err}"),
FastExcelErrorKind::Internal(err) => write!(f, "fastexcel error: {err}"),
}
}
}
#[derive(Debug)]
pub(crate) struct FastExcelError {
pub kind: FastExcelErrorKind,
context: Vec<String>,
}
pub(crate) trait ErrorContext {
fn with_context<S: ToString, F>(self, ctx_fn: F) -> Self
where
F: FnOnce() -> S;
}
impl FastExcelError {
pub(crate) fn new(kind: FastExcelErrorKind) -> Self {
Self {
kind,
context: vec![],
}
}
}
impl Display for FastExcelError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{kind}", kind = self.kind)?;
if !self.context.is_empty() {
writeln!(f, "\nContext:")?;
self.context
.iter()
.enumerate()
.try_for_each(|(idx, ctx_value)| writeln!(f, " {idx}: {ctx_value}"))?;
}
Ok(())
}
}
impl Error for FastExcelError {}
impl ErrorContext for FastExcelError {
fn with_context<S: ToString, F>(mut self, ctx_fn: F) -> Self
where
F: FnOnce() -> S,
{
self.context.push(ctx_fn().to_string());
self
}
}
impl From<FastExcelErrorKind> for FastExcelError {
fn from(kind: FastExcelErrorKind) -> Self {
FastExcelError::new(kind)
}
}
impl From<XlsxError> for FastExcelError {
fn from(err: XlsxError) -> Self {
FastExcelErrorKind::CalamineError(calamine::Error::Xlsx(err)).into()
}
}
pub(crate) type FastExcelResult<T> = Result<T, FastExcelError>;
impl<T> ErrorContext for FastExcelResult<T> {
fn with_context<S: ToString, F>(self, ctx_fn: F) -> Self
where
F: FnOnce() -> S,
{
match self {
Ok(_) => self,
Err(e) => Err(e.with_context(ctx_fn)),
}
}
}
/// Contains Python versions of our custom errors
pub(crate) mod py_errors {
use super::FastExcelErrorKind;
use crate::error;
use pyo3::{PyErr, PyResult, create_exception, exceptions::PyException};
// Base fastexcel error
create_exception!(
_fastexcel,
FastExcelError,
PyException,
"The base class for all fastexcel errors"
);
// Unsupported column type
create_exception!(
_fastexcel,
UnsupportedColumnTypeCombinationError,
FastExcelError,
"Column contains an unsupported type combination"
);
// Cannot retrieve cell data
create_exception!(
_fastexcel,
CannotRetrieveCellDataError,
FastExcelError,
"Data for a given cell cannot be retrieved"
);
// Calamine cell error
create_exception!(
_fastexcel,
CalamineCellError,
FastExcelError,
"calamine returned an error regarding the content of the cell"
);
// Calamine error
create_exception!(
_fastexcel,
CalamineError,
FastExcelError,
"Generic calamine error"
);
// Sheet not found
create_exception!(
_fastexcel,
SheetNotFoundError,
FastExcelError,
"Sheet was not found"
);
// Sheet not found
create_exception!(
_fastexcel,
ColumnNotFoundError,
FastExcelError,
"Column was not found"
);
// Arrow error
create_exception!(
_fastexcel,
ArrowError,
FastExcelError,
"Generic arrow error"
);
// Invalid parameters
create_exception!(
_fastexcel,
InvalidParametersError,
FastExcelError,
"Provided parameters are invalid"
);
// Internal error
create_exception!(
_fastexcel,
InternalError,
FastExcelError,
"Internal fastexcel error"
);
impl From<error::FastExcelError> for PyErr {
fn from(err: error::FastExcelError) -> Self {
let message = err.to_string();
match err.kind {
FastExcelErrorKind::UnsupportedColumnTypeCombination(_) => {
UnsupportedColumnTypeCombinationError::new_err(message)
}
FastExcelErrorKind::CannotRetrieveCellData(_, _) => {
CannotRetrieveCellDataError::new_err(message)
}
FastExcelErrorKind::CalamineCellError(_) => CalamineCellError::new_err(message),
FastExcelErrorKind::CalamineError(_) => CalamineError::new_err(message),
FastExcelErrorKind::SheetNotFound(_) => SheetNotFoundError::new_err(message),
FastExcelErrorKind::ColumnNotFound(_) => ColumnNotFoundError::new_err(message),
FastExcelErrorKind::ArrowError(_) => ArrowError::new_err(message),
FastExcelErrorKind::InvalidParameters(_) => {
InvalidParametersError::new_err(message)
}
FastExcelErrorKind::Internal(_) => ArrowError::new_err(message),
}
}
}
pub(crate) trait IntoPyResult {
type Inner;
fn into_pyresult(self) -> PyResult<Self::Inner>;
}
impl<T> IntoPyResult for super::FastExcelResult<T> {
type Inner = T;
fn into_pyresult(self) -> PyResult<Self::Inner> {
self.map_err(Into::into)
}
}
}