1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use super::*;
#[cfg(feature = "std")]
impl core::fmt::Display for ErrorData {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        if let Some(c) = &self.cause {
            c.fmt(f)
        } else {
            let c = self
                .payload
                .iter()
                .find(|p| matches!(p, PayloadEntry::Cause { .. }));
            if let Some(PayloadEntry::Cause { display, .. }) = c {
                f.pad(display)
            } else {
                // Best we can do, I suppose.
                write!(f, "Ockam error [{}] at {}", self.code, self.source_loc)
            }
        }
    }
}

#[cfg(feature = "std")]
impl core::fmt::Debug for ErrorData {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        writeln!(f, "Ockam Error [{}] at {}", self.code, self.source_loc)?;
        writeln!(f, "--- Details ---")?;
        if let Some(c) = &self.cause {
            // TODO: iterate the cause chain, print the whole thing outp
            writeln!(f, "- Caused by: {}", c)?;
        }
        for e in self.payload.iter() {
            match e {
                PayloadEntry::Info(k, v) => {
                    writeln!(f, "- info {:?}: {}", k, v)?;
                }
                PayloadEntry::Cause {
                    display,
                    debug,
                    type_name,
                } => {
                    writeln!(f, "- possible root cause `{}`: {}", type_name, display)?;
                    writeln!(f, "  debug output: ")?;
                    for line in debug.lines() {
                        writeln!(f, "    {}", line)?;
                    }
                }
            }
        }

        if let Some(local) = self.local.as_ref() {
            for e in local {
                match e {
                    #[cfg(all(feature = "std", feature = "tracing-error"))]
                    LocalPayloadEntry::Spantrace(t) => {
                        writeln!(f, "--- Captured Spantrace ---")?;
                        writeln!(f, "{}", t)?;
                    }
                    #[cfg(all(feature = "std", feature = "backtrace"))]
                    LocalPayloadEntry::Backtrace(t) => {
                        writeln!(f, "--- Captured Backtrace ---")?;
                        // TODO: Finish up filtering out pointless frames -- see
                        // https://gist.github.com/thomcc/0e9f1cc4dd9f6f12943a158eeb848f7e
                        writeln!(f, "{:?}", t)?;
                    }
                    #[allow(unreachable_patterns)]
                    _ => {}
                }
            }
        }
        writeln!(f, "--------------------------")?;
        Ok(())
    }
}

#[cfg(not(feature = "std"))]
impl core::fmt::Display for ErrorData {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        // TODO
        write!(f, "Ockam Error [{}]", self.code)
    }
}
#[cfg(not(feature = "std"))]
impl core::fmt::Debug for ErrorData {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        // TODO
        write!(f, "Ockam Error [{}]", self.code)
    }
}