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
use std::fmt::{Display, Formatter};
use anyhow::Error;
use thiserror::Error as ThisError;
#[derive(Debug)]
pub enum ErrString {
Owned(String),
Borrowed(&'static str),
}
impl From<&'static str> for ErrString {
fn from(msg: &'static str) -> Self {
if std::env::var("POLARS_PANIC_ON_ERR").is_ok() {
panic!("{}", msg)
} else {
ErrString::Borrowed(msg)
}
}
}
impl From<String> for ErrString {
fn from(msg: String) -> Self {
if std::env::var("POLARS_PANIC_ON_ERR").is_ok() {
panic!("{}", msg)
} else {
ErrString::Owned(msg)
}
}
}
impl Display for ErrString {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let msg = match self {
ErrString::Owned(msg) => msg.as_str(),
ErrString::Borrowed(msg) => msg,
};
write!(f, "{msg}")
}
}
#[derive(Debug, ThisError)]
pub enum PolarsError {
#[error(transparent)]
ArrowError(Box<ArrowError>),
#[error("Invalid operation {0}")]
InvalidOperation(ErrString),
#[error("Data types don't match: {0}")]
SchemaMisMatch(ErrString),
#[error("Not found: {0}")]
NotFound(ErrString),
#[error("Lengths don't match: {0}")]
ShapeMisMatch(ErrString),
#[error("{0}")]
ComputeError(ErrString),
#[error("Such empty...: {0}")]
NoData(ErrString),
#[error(transparent)]
Io(#[from] std::io::Error),
#[error("DuplicateError: {0}")]
Duplicate(ErrString),
}
impl From<ArrowError> for PolarsError {
fn from(err: ArrowError) -> Self {
Self::ArrowError(Box::new(err))
}
}
impl From<anyhow::Error> for PolarsError {
fn from(err: Error) -> Self {
PolarsError::ComputeError(format!("{err:?}").into())
}
}
impl From<polars_arrow::error::PolarsError> for PolarsError {
fn from(err: polars_arrow::error::PolarsError) -> Self {
PolarsError::ComputeError(format!("{err:?}").into())
}
}
#[cfg(any(feature = "strings", feature = "temporal"))]
impl From<regex::Error> for PolarsError {
fn from(err: regex::Error) -> Self {
PolarsError::ComputeError(format!("regex error: {err:?}").into())
}
}
pub type PolarsResult<T> = std::result::Result<T, PolarsError>;
pub use arrow::error::Error as ArrowError;