Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-python/src/error.rs
7884 views
1
use std::error::Error;
2
use std::fmt::{Debug, Display, Formatter};
3
use std::io::ErrorKind;
4
5
use polars::prelude::PolarsError;
6
use polars_error::PolarsWarning;
7
use pyo3::PyTypeInfo;
8
use pyo3::exceptions::{
9
PyDeprecationWarning, PyFileExistsError, PyFileNotFoundError, PyIOError, PyPermissionError,
10
PyRuntimeError, PyUserWarning,
11
};
12
use pyo3::prelude::*;
13
14
use crate::Wrap;
15
use crate::exceptions::{
16
CategoricalRemappingWarning, ColumnNotFoundError, ComputeError, DuplicateError,
17
InvalidOperationError, MapWithoutReturnDtypeWarning, NoDataError, OutOfBoundsError,
18
SQLInterfaceError, SQLSyntaxError, SchemaError, SchemaFieldNotFoundError, ShapeError,
19
StringCacheMismatchError, StructFieldNotFoundError,
20
};
21
22
pub enum PyPolarsErr {
23
Polars(PolarsError),
24
Python(PyErr),
25
Other(String),
26
}
27
28
impl Error for PyPolarsErr {
29
fn source(&self) -> Option<&(dyn Error + 'static)> {
30
match self {
31
Self::Polars(err) => Some(err),
32
Self::Python(err) => Some(err),
33
Self::Other(_) => None,
34
}
35
}
36
}
37
38
impl std::fmt::Display for PyPolarsErr {
39
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
40
match self {
41
Self::Polars(err) => Display::fmt(err, f),
42
Self::Python(err) => Display::fmt(err, f),
43
Self::Other(err) => write!(f, "{err}"),
44
}
45
}
46
}
47
48
impl From<PolarsError> for PyPolarsErr {
49
fn from(err: PolarsError) -> Self {
50
PyPolarsErr::Polars(err)
51
}
52
}
53
54
impl From<PyErr> for PyPolarsErr {
55
fn from(err: PyErr) -> Self {
56
PyPolarsErr::Python(err)
57
}
58
}
59
60
impl From<PyPolarsErr> for PyErr {
61
fn from(err: PyPolarsErr) -> PyErr {
62
use PyPolarsErr::*;
63
match err {
64
Polars(err) => match err {
65
PolarsError::AssertionError(err) => {
66
pyo3::exceptions::PyAssertionError::new_err(err.to_string())
67
},
68
PolarsError::ColumnNotFound(name) => ColumnNotFoundError::new_err(name.to_string()),
69
PolarsError::ComputeError(err) => ComputeError::new_err(err.to_string()),
70
PolarsError::Duplicate(err) => DuplicateError::new_err(err.to_string()),
71
PolarsError::InvalidOperation(err) => {
72
InvalidOperationError::new_err(err.to_string())
73
},
74
PolarsError::IO { error, msg } => {
75
let msg = if let Some(msg) = msg {
76
msg.to_string()
77
} else {
78
error.to_string()
79
};
80
match error.kind() {
81
ErrorKind::NotFound => PyFileNotFoundError::new_err(msg),
82
ErrorKind::PermissionDenied => PyPermissionError::new_err(msg),
83
ErrorKind::AlreadyExists => PyFileExistsError::new_err(msg),
84
_ => PyIOError::new_err(msg),
85
}
86
},
87
PolarsError::NoData(err) => NoDataError::new_err(err.to_string()),
88
PolarsError::OutOfBounds(err) => OutOfBoundsError::new_err(err.to_string()),
89
PolarsError::SQLInterface(name) => SQLInterfaceError::new_err(name.to_string()),
90
PolarsError::SQLSyntax(name) => SQLSyntaxError::new_err(name.to_string()),
91
PolarsError::SchemaFieldNotFound(name) => {
92
SchemaFieldNotFoundError::new_err(name.to_string())
93
},
94
PolarsError::SchemaMismatch(err) => SchemaError::new_err(err.to_string()),
95
PolarsError::ShapeMismatch(err) => ShapeError::new_err(err.to_string()),
96
PolarsError::StringCacheMismatch(err) => {
97
StringCacheMismatchError::new_err(err.to_string())
98
},
99
PolarsError::StructFieldNotFound(name) => {
100
StructFieldNotFoundError::new_err(name.to_string())
101
},
102
PolarsError::Context { .. } => {
103
let tmp = PyPolarsErr::Polars(err.context_trace());
104
PyErr::from(tmp)
105
},
106
PolarsError::Python { error } => error.0,
107
},
108
Python(err) => err,
109
err => PyRuntimeError::new_err(format!("{:?}", &err)),
110
}
111
}
112
}
113
114
impl Debug for PyPolarsErr {
115
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
116
use PyPolarsErr::*;
117
match self {
118
Polars(err) => write!(f, "{err:?}"),
119
Python(err) => write!(f, "{err:?}"),
120
Other(err) => write!(f, "BindingsError: {err:?}"),
121
}
122
}
123
}
124
125
#[macro_export]
126
macro_rules! raise_err(
127
($msg:expr, $err:ident) => {{
128
Err(PolarsError::$err($msg.into())).map_err(PyPolarsErr::from)?;
129
unreachable!()
130
}}
131
);
132
133
impl<'py> IntoPyObject<'py> for Wrap<PolarsWarning> {
134
type Target = PyAny;
135
type Output = Bound<'py, Self::Target>;
136
type Error = std::convert::Infallible;
137
138
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
139
match self.0 {
140
PolarsWarning::CategoricalRemappingWarning => {
141
Ok(CategoricalRemappingWarning::type_object(py).into_any())
142
},
143
PolarsWarning::MapWithoutReturnDtypeWarning => {
144
Ok(MapWithoutReturnDtypeWarning::type_object(py).into_any())
145
},
146
PolarsWarning::UserWarning => Ok(PyUserWarning::type_object(py).into_any()),
147
PolarsWarning::Deprecation => Ok(PyDeprecationWarning::type_object(py).into_any()),
148
}
149
}
150
}
151
152