pyo3_polars/
error.rs

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