Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-utils/src/error.rs
6939 views
1
use std::borrow::Cow;
2
use std::fmt::{Display, Formatter};
3
4
use crate::config::verbose;
5
use crate::format_pl_smallstr;
6
7
type ErrString = Cow<'static, str>;
8
9
#[derive(Debug)]
10
pub enum PolarsUtilsError {
11
ComputeError(ErrString),
12
}
13
14
impl Display for PolarsUtilsError {
15
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
16
match self {
17
PolarsUtilsError::ComputeError(s) => {
18
let s = s.as_ref();
19
write!(f, "{s}")
20
},
21
}
22
}
23
}
24
25
pub type Result<T> = std::result::Result<T, PolarsUtilsError>;
26
27
/// Utility whose Display impl truncates the string unless POLARS_VERBOSE is set.
28
pub struct TruncateErrorDetail<'a>(pub &'a str);
29
30
impl std::fmt::Display for TruncateErrorDetail<'_> {
31
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
32
let maybe_truncated = if verbose() {
33
self.0
34
} else {
35
// Clamp the output on non-verbose
36
&self.0[..self.0.len().min(4096)]
37
};
38
39
f.write_str(maybe_truncated)?;
40
41
if maybe_truncated.len() != self.0.len() {
42
let n_more = self.0.len() - maybe_truncated.len();
43
f.write_str(" ...(set POLARS_VERBOSE=1 to see full response (")?;
44
f.write_str(&format_pl_smallstr!("{}", n_more))?;
45
f.write_str(" more characters))")?;
46
};
47
48
Ok(())
49
}
50
}
51
52