Skip to main content

zeroclaw_log/
chain.rs

1//! Anyhow error-chain rendering helper.
2//!
3//! Centralizes the `format!("{err:#}")` invocation so future evolution
4//! (e.g. plugging in `LeakDetector` to redact secrets from error messages)
5//! happens in one place.
6
7/// Render an `anyhow::Error` with its full `.context()` chain.
8///
9/// Uses the alternate Display formatter (`{:#}`) which walks the chain.
10/// Plain `{}` only prints the leaf message, losing every context layer.
11#[must_use]
12pub fn display_chain(err: &anyhow::Error) -> String {
13    format!("{err:#}")
14}
15
16#[cfg(test)]
17mod tests {
18    use super::*;
19    use anyhow::Context;
20
21    #[test]
22    fn display_chain_walks_context() {
23        let leaf: anyhow::Result<()> = Err(anyhow::Error::msg("connection refused"));
24        let err = leaf
25            .context("failed to dial provider")
26            .context("processing turn")
27            .unwrap_err();
28        let s = display_chain(&err);
29        assert!(s.contains("processing turn"));
30        assert!(s.contains("failed to dial provider"));
31        assert!(s.contains("connection refused"));
32    }
33}