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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use std::error::Error;
use std::fmt::Display;
use std::fmt::Formatter;

use serde::Deserialize;
use serde::Serialize;

/// AnyError is a serializable wrapper `Error`.
///
/// It is can be used to convert other `Error` into a serializable Error for transmission,
/// with most necessary info kept.
///
/// ```
/// # use std::fmt;
/// # use anyerror::AnyError;
/// let err = fmt::Error {};
/// let e = AnyError::new(&err).add_context(|| "running test");
/// assert_eq!(
///     "core::fmt::Error: an error occurred when formatting an argument while: running test",
///     e.to_string()
/// );
/// ```
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Default)]
#[cfg_attr(
    feature = "rkyv",
    derive(rkyv::Archive, rkyv::Deserialize, rkyv::Serialize),
    archive(
        check_bytes,
        bound(serialize = "__S: rkyv::ser::ScratchSpace + rkyv::ser::Serializer")
    ),
    archive_attr(check_bytes(
        bound = "__C: rkyv::validation::ArchiveContext, <__C as rkyv::Fallible>::Error: std::error::Error"
    ),)
)]
pub struct AnyError {
    typ: Option<String>,

    msg: String,

    #[cfg_attr(feature = "rkyv", omit_bounds, archive_attr(omit_bounds))]
    source: Option<Box<AnyError>>,

    /// context provides additional info about the context when a error happened.
    context: Vec<String>,

    backtrace: Option<String>,
}

impl Display for AnyError {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        if let Some(t) = &self.typ {
            write!(f, "{}: ", t)?;
        }

        write!(f, "{}", self.msg)?;

        for (i, ctx) in self.context.iter().enumerate() {
            if f.alternate() {
                writeln!(f)?;
                write!(f, "    while: {}", ctx)?;
            } else {
                if i > 0 {
                    write!(f, ",")?;
                }
                write!(f, " while: {}", ctx)?;
            }
        }

        #[allow(clippy::collapsible_else_if)]
        if f.alternate() {
            if let Some(ref s) = self.source {
                writeln!(f)?;
                write!(f, "source: ")?;
                write!(f, "{:#}", s)?;
            }
        } else {
            if let Some(ref s) = self.source {
                write!(f, "; source: {}", s)?;
            }
        }

        Ok(())
    }
}

impl std::fmt::Debug for AnyError {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        <Self as Display>::fmt(self, f)?;

        if let Some(ref b) = self.backtrace {
            write!(f, "\nbacktrace:\n{}", b)?;
        }
        Ok(())
    }
}

impl std::error::Error for AnyError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match &self.source {
            Some(x) => Some(x.as_ref()),
            None => None,
        }
    }
}

#[cfg(feature = "anyhow")]
impl From<anyhow::Error> for AnyError {
    fn from(a: anyhow::Error) -> Self {
        // NOTE: this does not work: anyhow::Error does not impl std::Error;
        //       backtrace is lost after converting it to &dyn Error with stable rust.
        // AnyError::from_dyn(a.as_ref(), None)

        let source = a.source().map(|x| Box::new(AnyError::from_dyn(x, None)));
        #[cfg(feature = "backtrace")]
        let bt = Some({
            let bt = a.backtrace();
            format!("{:?}", bt)
        });

        #[cfg(not(feature = "backtrace"))]
        let bt = None;

        Self {
            typ: None,
            msg: a.to_string(),
            source,
            context: vec![],
            backtrace: bt,
        }
    }
}

impl<E> From<&E> for AnyError
where E: std::error::Error + 'static
{
    fn from(e: &E) -> Self {
        AnyError::new(e)
    }
}

impl AnyError {
    /// Create an ad-hoc error with a string message
    pub fn error(msg: impl ToString) -> Self {
        Self {
            typ: None,
            msg: msg.to_string(),
            source: None,
            context: vec![],
            backtrace: None,
        }
    }

    /// Convert some `Error` to AnyError.
    ///
    /// - If there is a `source()` in the input error, it is also converted to AnyError, recursively.
    /// - A new backtrace will be built if there is not.
    pub fn new<E>(e: &E) -> Self
    where E: Error + 'static {
        let q: &(dyn Error + 'static) = e;

        let x = q.downcast_ref::<AnyError>();
        let typ = match x {
            Some(ae) => ae.typ.clone(),
            None => Some(std::any::type_name::<E>().to_string()),
        };

        Self::from_dyn(e, typ)
    }

    pub fn from_dyn(e: &(dyn Error + 'static), typ: Option<String>) -> Self {
        let x = e.downcast_ref::<AnyError>();

        return match x {
            Some(ae) => ae.clone(),
            None => {
                #[cfg(feature = "backtrace")]
                let bt = crate::bt::error_backtrace_str(e);

                #[cfg(not(feature = "backtrace"))]
                let bt = None;

                let source = e.source().map(|x| Box::new(AnyError::from_dyn(x, None)));

                Self {
                    typ,
                    msg: e.to_string(),
                    source,
                    context: vec![],
                    backtrace: bt,
                }
            }
        };
    }

    #[cfg(feature = "backtrace")]
    #[must_use]
    pub fn with_backtrace(mut self) -> Self {
        if self.backtrace.is_some() {
            return self;
        }

        self.backtrace = Some(crate::bt::new_str());
        self
    }

    #[must_use]
    pub fn add_context<D: Display, F: FnOnce() -> D>(mut self, ctx: F) -> Self {
        self.context.push(format!("{}", ctx()));
        self
    }

    pub fn get_type(&self) -> Option<&str> {
        self.typ.as_ref().map(|x| x as _)
    }

    pub fn backtrace(&self) -> Option<&str> {
        self.backtrace.as_ref().map(|x| x as _)
    }
}

/// Generate backtrace in string if feature `backtrace` is enabled.
/// Otherwise it returns None.
pub fn backtrace_str() -> Option<String> {
    #[cfg(feature = "backtrace")]
    return Some(crate::bt::new_str());

    #[cfg(not(feature = "backtrace"))]
    return None;
}