Skip to main content

pyo3_polars/
error.rs

1use std::fmt::{Debug, Formatter};
2
3use polars::prelude::PolarsError;
4use pyo3::create_exception;
5use pyo3::exceptions::{
6    PyAssertionError, PyException, PyIOError, PyIndexError, PyRuntimeError, PyValueError,
7};
8use pyo3::prelude::*;
9use thiserror::Error;
10
11#[derive(Error)]
12pub enum PyPolarsErr {
13    #[error(transparent)]
14    Polars(#[from] PolarsError),
15    #[error("{0}")]
16    Other(String),
17}
18
19impl std::convert::From<PyPolarsErr> for PyErr {
20    fn from(err: PyPolarsErr) -> PyErr {
21        fn convert(err: PolarsError) -> PyErr {
22            match err {
23                PolarsError::AssertionError(error) => PyAssertionError::new_err(error.to_string()),
24                PolarsError::ComputeError(err) => ComputeError::new_err(err.to_string()),
25                PolarsError::NoData(err) => NoDataError::new_err(err.to_string()),
26                PolarsError::ShapeMismatch(err) => ShapeError::new_err(err.to_string()),
27                PolarsError::SchemaMismatch(err) => SchemaError::new_err(err.to_string()),
28                PolarsError::IO { error, .. } => PyIOError::new_err(error.to_string()),
29                PolarsError::OutOfBounds(err) => PyIndexError::new_err(err.to_string()),
30                PolarsError::InvalidOperation(err) => PyValueError::new_err(err.to_string()),
31                PolarsError::Duplicate(err) => DuplicateError::new_err(err.to_string()),
32                PolarsError::ColumnNotFound(err) => ColumnNotFound::new_err(err.to_string()),
33                PolarsError::SchemaFieldNotFound(err) => {
34                    SchemaFieldNotFound::new_err(err.to_string())
35                },
36                PolarsError::StructFieldNotFound(err) => {
37                    StructFieldNotFound::new_err(err.to_string())
38                },
39                PolarsError::StringCacheMismatch(err) => {
40                    StringCacheMismatchError::new_err(err.to_string())
41                },
42                PolarsError::SQLInterface(err) => SQLInterface::new_err(err.to_string()),
43                PolarsError::SQLSyntax(err) => SQLSyntax::new_err(err.to_string()),
44                PolarsError::Context { .. } | PolarsError::ExprContext { .. } => {
45                    let tmp = PyPolarsErr::Polars(err.context_trace());
46                    PyErr::from(tmp)
47                },
48                PolarsError::Python { error } => error.0,
49            }
50        }
51
52        use PyPolarsErr::*;
53        match err {
54            Polars(err) => convert(err),
55            _ => PyRuntimeError::new_err(format!("{err:?}")),
56        }
57    }
58}
59
60impl Debug for PyPolarsErr {
61    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
62        use PyPolarsErr::*;
63        match self {
64            Polars(err) => write!(f, "{err:?}"),
65            Other(err) => write!(f, "BindingsError: {err:?}"),
66        }
67    }
68}
69
70create_exception!(exceptions, AssertionError, PyException);
71create_exception!(exceptions, ColumnNotFound, PyException);
72create_exception!(exceptions, SchemaFieldNotFound, PyException);
73create_exception!(exceptions, StructFieldNotFound, PyException);
74create_exception!(exceptions, ComputeError, PyException);
75create_exception!(exceptions, NoDataError, PyException);
76create_exception!(exceptions, ShapeError, PyException);
77create_exception!(exceptions, SchemaError, PyException);
78create_exception!(exceptions, DuplicateError, PyException);
79create_exception!(exceptions, StringCacheMismatchError, PyException);
80create_exception!(exceptions, SQLInterface, PyException);
81create_exception!(exceptions, SQLSyntax, PyException);