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 { error, .. } => convert(*error),
45                PolarsError::Python { error } => error.0,
46            }
47        }
48
49        use PyPolarsErr::*;
50        match err {
51            Polars(err) => convert(err),
52            _ => PyRuntimeError::new_err(format!("{err:?}")),
53        }
54    }
55}
56
57impl Debug for PyPolarsErr {
58    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
59        use PyPolarsErr::*;
60        match self {
61            Polars(err) => write!(f, "{err:?}"),
62            Other(err) => write!(f, "BindingsError: {err:?}"),
63        }
64    }
65}
66
67create_exception!(exceptions, AssertionError, PyException);
68create_exception!(exceptions, ColumnNotFound, PyException);
69create_exception!(exceptions, SchemaFieldNotFound, PyException);
70create_exception!(exceptions, StructFieldNotFound, PyException);
71create_exception!(exceptions, ComputeError, PyException);
72create_exception!(exceptions, NoDataError, PyException);
73create_exception!(exceptions, ShapeError, PyException);
74create_exception!(exceptions, SchemaError, PyException);
75create_exception!(exceptions, DuplicateError, PyException);
76create_exception!(exceptions, StringCacheMismatchError, PyException);
77create_exception!(exceptions, SQLInterface, PyException);
78create_exception!(exceptions, SQLSyntax, PyException);