Skip to main content

polars_utils/
error.rs

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