Skip to main content

cognis_trace/
error.rs

1//! Crate-wide error type. Exporter failures are *never* propagated into
2//! the user's main code path — they are logged and counted. `TraceError`
3//! exists for diagnostic surfaces (`stats()`, builder validation).
4
5use thiserror::Error;
6
7/// Errors surfaced by `cognis-trace`. Exporter-side failures are logged
8/// and counted; this enum exists for builder validation and stats.
9#[derive(Debug, Error)]
10pub enum TraceError {
11    /// Required environment variable is unset.
12    #[error("missing required env var: {0}")]
13    MissingEnvVar(String),
14
15    /// Configuration value is invalid (e.g. malformed URL).
16    #[error("invalid config: {0}")]
17    InvalidConfig(String),
18
19    /// Network error sending to a backend.
20    #[cfg(feature = "langfuse")]
21    #[error("network error sending to {backend}: {source}")]
22    Network {
23        /// Backend name (e.g. "langfuse").
24        backend: &'static str,
25        /// Underlying reqwest error.
26        #[source]
27        source: reqwest::Error,
28    },
29
30    /// Backend returned a non-2xx status.
31    #[error("backend {backend} returned {status}: {body}")]
32    BackendStatus {
33        /// Backend name.
34        backend: &'static str,
35        /// HTTP status code.
36        status: u16,
37        /// Response body (truncated for log safety).
38        body: String,
39    },
40
41    /// Exporter queue overflowed; events were dropped.
42    #[error("queue overflowed; {dropped} events dropped (backend: {backend})")]
43    QueueOverflow {
44        /// Backend name.
45        backend: &'static str,
46        /// Number of events dropped.
47        dropped: usize,
48    },
49
50    /// Exporter does not support a particular feature (scores, prompts, …).
51    #[error("backend does not support: {0}")]
52    Unsupported(&'static str),
53
54    /// JSON serialization failed.
55    #[error("serialization: {0}")]
56    Serde(#[from] serde_json::Error),
57}
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn missing_env_var_displays_name() {
65        let e = TraceError::MissingEnvVar("LANGFUSE_PUBLIC_KEY".into());
66        assert_eq!(
67            e.to_string(),
68            "missing required env var: LANGFUSE_PUBLIC_KEY"
69        );
70    }
71
72    #[test]
73    fn unsupported_carries_feature_name() {
74        let e = TraceError::Unsupported("scores");
75        assert_eq!(e.to_string(), "backend does not support: scores");
76    }
77}