Skip to main content

troupekit_trace/
error.rs

1use std::error::Error;
2use std::fmt;
3
4const SHOWN_BYTES_MAX: usize = 64;
5
6/// Errors from parsing trace settings or building a tracer.
7/// Input: caller settings. Output: one variant naming what was wrong.
8/// Later releases may add variants.
9#[derive(Clone, Debug, PartialEq, Eq)]
10#[non_exhaustive]
11pub enum TraceError {
12    UnknownTrace(String),
13    TooManyNames(usize),
14    TooManySecrets(usize),
15    MissingSink,
16    InsideSink,
17}
18
19impl TraceError {
20    pub(crate) fn unknown(text: &str) -> TraceError {
21        TraceError::UnknownTrace(crate::value::cut_to(text, SHOWN_BYTES_MAX).to_owned())
22    }
23}
24
25impl fmt::Display for TraceError {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        match self {
28            TraceError::UnknownTrace(name) => write!(f, "unknown trace category `{name}`"),
29            TraceError::TooManyNames(n) => write!(f, "{n} trace names, at most 64"),
30            TraceError::TooManySecrets(n) => write!(f, "{n} scrubbed secrets, at most 1024"),
31            TraceError::MissingSink => f.write_str("a tracer needs a sink"),
32            TraceError::InsideSink => {
33                f.write_str("a tracer cannot be built inside a sink write or flush")
34            }
35        }
36    }
37}
38
39impl Error for TraceError {}