1use std::{error, fmt, io};
2
3#[derive(Debug)]
4pub struct Error {
5 message: String,
6 source: Option<Box<dyn error::Error + Send + Sync + 'static>>,
7 span: tracing_error::SpanTrace,
8}
9
10impl fmt::Display for Error {
11 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
12 writeln!(f, "{}", self.message)?;
14
15 if let Some(source) = &self.source {
16 writeln!(f, "{}", source)?;
17 }
18
19 write!(f, "Trace:\n{}", self.span)
20 }
21}
22
23impl error::Error for Error {
24 fn source(&self) -> Option<&(dyn error::Error + 'static)> {
25 match &self.source {
26 Some(source) => {
27 let source: &(dyn error::Error + 'static) = &**source;
28 Some(source)
29 }
30 None => None,
31 }
32 }
33}
34
35impl Error {
36 pub(crate) fn wrap_std(source: io::Error) -> io::Error {
37 let kind = source.kind();
38 let message = source.to_string();
39 let source = source.into_inner();
40
41 io::Error::new(
42 kind,
43 Error {
44 message,
45 source,
46 span: tracing_error::SpanTrace::capture(),
47 },
48 )
49 }
50}