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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
use crate::backtrace::Frame;
use crate::term::{self, Colorized};
use crate::WITCHER_FULLSTACK;
use crate::{Result, StdError};
use std::convert::From;
use std::fmt::{self, Debug, Display, Formatter};

static ERROR_TYPE: &str = "witcher::Error";
static STDERROR_TYPE: &str = "std::error::Error";
static LONG_ERROR_TYPE: &str = "witcher::error::Error";

/// `Error` is a wrapper providing additional context and chaining of errors.
///
/// `Error` provides the following benefits
///  - ensures a backtrace will be taken at the earliest opportunity
///  - ensures that the error type is threadsafe and has a static lifetime
///  - provides matching on inner error types
///
/// Context comes in two forms. First every time an error is wrapped you have the
/// opportunity to add an additional message. Finally a simplified stack trace is
/// automatically provided that narrows in on your actual code ignoring the wind up
/// and wind down that resides in the Rust std libraries and other dependencies
/// allowing you to focus on your code.
///
/// Saftey: data layout ensured to be consistent with repr(C) for raw conversions.
pub struct Error {
    // Error message which will either be additional context for the inner error
    // or in the case where this error was created from `new` will be the only
    // error message.
    msg: String,

    // Type name here will refer to the inner error in the case where
    // inner error is Some and is an external type else it will be `Error`.
    type_name: String,

    // Backtrace frames that have been cleaned up
    backtrace: Vec<Frame>,

    // The original error in the case where we're wrapping an external error or
    // an `Error` in the case where we're wrapping another `Error`.
    inner: Option<Box<dyn StdError + Send + Sync + 'static>>,
}
impl Error {
    /// Create a new error instance wrapped in a result
    ///
    pub fn raw(msg: &str) -> Self {
        Self { msg: msg.to_string(), type_name: String::from(ERROR_TYPE), backtrace: crate::backtrace::new(), inner: None }
    }

    /// Wrap the given error and include a contextual message for the error.
    ///
    pub fn wrapr<E>(err: E, msg: &str) -> Self
    where
        E: StdError + Send + Sync + 'static,
    {
        Self { msg: msg.to_string(), type_name: Error::name(&err), backtrace: crate::backtrace::new(), inner: Some(Box::new(err)) }
    }

    /// Create a new error instance wrapped in a result
    ///
    pub fn new<T>(msg: &str) -> Result<T> {
        Err(Error::raw(msg))
    }

    /// Wrap the given error and include a contextual message for the error.
    ///
    pub fn wrap<T, E>(err: E, msg: &str) -> Result<T>
    where
        E: StdError + Send + Sync + 'static,
    {
        Err(Error::wrapr(err, msg))
    }

    /// Return the first external error of the error chain for downcasting.
    /// The intent is that when writing application code there are cases where your more
    /// interested in reacting to an external failure.
    /// If there is no external error then you'll get the last `Error` in the chain.
    pub fn ext(&self) -> &(dyn StdError + 'static) {
        let mut stderr: &(dyn StdError + 'static) = self;
        let mut source = self.source();
        while let Some(err) = source {
            stderr = err;
            if !err.is::<Error>() {
                break;
            }
            source = err.source();
        }
        stderr
    }

    /// Return the last of the error chain for downcasting.
    /// This will follow the chain of source errors down to the last and return it.
    /// If this error is the only error it will be returned instead.
    pub fn last(&self) -> &(dyn StdError + 'static) {
        let mut err: &(dyn StdError + 'static) = self;
        let mut source = self.source();
        while let Some(e) = source {
            err = e;
            source = e.source();
        }
        err
    }

    /// Implemented directly on the `Error` type to reduce casting required
    pub fn is<T: StdError + 'static>(&self) -> bool {
        <dyn StdError + 'static>::is::<T>(self)
    }

    /// Implemented directly on the `Error` type to reduce casting required
    pub fn downcast_ref<T: StdError + 'static>(&self) -> Option<&T> {
        <dyn StdError + 'static>::downcast_ref::<T>(self)
    }

    /// Implemented directly on the `Error` type to reduce casting required
    pub fn downcast_mut<T: StdError + 'static>(&mut self) -> Option<&mut T> {
        <dyn StdError + 'static>::downcast_mut::<T>(self)
    }

    /// Implemented directly on the `Error` type to reduce casting required
    pub fn source(&self) -> Option<&(dyn StdError + 'static)> {
        self.as_ref().source()
    }

    /// Extract the name of the given error type and perform some clean up on the type
    fn name<T>(_: T) -> String {
        let mut name = std::any::type_name::<T>().to_string();

        // Strip off prefixes
        if name.starts_with('&') {
            name = String::from(name.trim_start_matches('&'));
        }

        if name.starts_with("dyn ") {
            name = String::from(name.trim_start_matches("dyn "));
        }

        // Strip off suffixes
        name = String::from(name.split('<').next().unwrap_or("<unknown>"));

        // Hide full Error path
        if name == LONG_ERROR_TYPE {
            name = String::from(ERROR_TYPE);
        }

        name
    }

    // Write out external errors
    fn write_std(&self, f: &mut Formatter<'_>, c: &Colorized, stderr: &dyn StdError) -> fmt::Result {
        let mut buf = format!(" cause: {}: {}", c.red(&self.type_name), c.red(stderr));
        let mut source = stderr.source();
        while let Some(inner) = source {
            if !buf.ends_with('\n') {
                buf += &"\n";
            }
            buf += &format!(" cause: {}: {}", c.red(STDERROR_TYPE), c.red(inner));
            source = inner.source();
        }
        if !buf.ends_with('\n') {
            buf += &"\n";
        }
        write!(f, "{}", buf)
    }

    fn write_frames(&self, f: &mut Formatter<'_>, c: &Colorized, parent: Option<&Error>, fullstack: bool) -> fmt::Result {
        let frames: Vec<&Frame> = if !fullstack {
            let frames: Vec<&Frame> = self.backtrace.iter().filter(|x| !x.is_dependency()).collect();
            match parent {
                Some(parent) => {
                    let len = frames.len();
                    let plen = parent.backtrace.iter().filter(|x| !x.is_dependency()).count();
                    frames.into_iter().take(len - plen).collect::<Vec<&Frame>>()
                }
                _ => frames,
            }

        // Fullstack `true` means don't filter anything
        } else {
            self.backtrace.iter().collect()
        };

        let len = frames.len();
        for (i, frame) in frames.iter().enumerate() {
            writeln!(f, "symbol: {}", c.cyan(&frame.symbol))?;
            write!(f, "    at: {}", frame.filename)?;

            if let Some(line) = frame.lineno {
                write!(f, ":{}", line)?;
                if let Some(column) = frame.column {
                    write!(f, ":{}", column)?;
                }
            }
            if i + 1 < len {
                writeln!(f)?;
            }
        }
        Ok(())
    }
}

// External trait implementations
// -------------------------------------------------------------------------------------------------

impl AsRef<dyn StdError> for Error {
    fn as_ref(&self) -> &(dyn StdError + 'static) {
        self
    }
}

impl StdError for Error {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        match &self.inner {
            Some(x) => Some(&**x),
            None => None,
        }
    }
}

/// Provides the same formatting for output as Display but includes the fullstack trace.
impl Debug for Error {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        // Setup controls for writing out errors
        let c = Colorized::new();
        let fullstack = term::var_enabled(WITCHER_FULLSTACK);

        // Push all `Error` instances to a vec then reverse
        let mut errors: Vec<&Error> = Vec::new();
        let mut source = self.source();
        errors.push(self);
        while let Some(stderr_ref) = source {
            if let Some(err) = stderr_ref.downcast_ref::<Error>() {
                errors.push(err);
                source = stderr_ref.source();
            } else {
                break;
            }
        }
        errors = errors.into_iter().rev().collect();

        // Pop them back off LIFO style
        let len = errors.len();
        for (i, err) in errors.iter().enumerate() {
            let parent: Option<&Error> = if i + 1 < len { Some(errors[i + 1]) } else { None };

            // Write out the error wrapper
            writeln!(f, " error: {}: {}", c.red(ERROR_TYPE), c.red(&err.msg))?;

            // Write out any std errors in order
            if i == 0 {
                if let Some(stderr) = (*err).source() {
                    err.write_std(f, &c, stderr)?;
                }
            }

            // Write out the frames minus those in the wrapping error
            err.write_frames(f, &c, parent, fullstack)?;
            if i + 1 < len {
                writeln!(f)?;
            }
        }
        Ok(())
    }
}

/// Provides formatting for output with frames filtered to just target code
impl Display for Error {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        if !f.alternate() {
            return write!(f, "{}", self.msg);
        }

        // Write out more detail
        let c = Colorized::new();
        let mut buf = String::new();
        buf += &format!(" error: {}", c.red(&self.msg));

        // Traverse the whole chain
        let mut source = self.source();
        while let Some(stderr) = source {
            if !buf.ends_with('\n') {
                buf += &"\n";
            }
            buf += &" cause: ".to_string();
            match stderr.downcast_ref::<Error>() {
                Some(err) => buf += &format!("{}", c.red(&err.msg)),
                _ => buf += &format!("{}", c.red(stderr)),
            }
            source = stderr.source();
        }
        write!(f, "{}", buf)
    }
}

// Unit tests
// -------------------------------------------------------------------------------------------------
#[cfg(test)]
mod tests {
    use super::*;
    use std::env;

    // Disable backtrace and colors
    use std::sync::Once;
    static INIT: Once = Once::new();
    pub fn initialize() {
        INIT.call_once(|| {
            env::set_var(crate::WITCHER_COLOR, "0");
            env::set_var("RUST_BACKTRACE", "0");
        });
    }

    struct TestError {
        msg: String,
        inner: Option<Box<TestError>>,
    }
    #[cfg(not(tarpaulin_include))]
    impl Debug for TestError {
        fn fmt(&self, f: &mut Formatter) -> fmt::Result {
            write!(f, "{}", self.msg)
        }
    }
    impl Display for TestError {
        fn fmt(&self, f: &mut Formatter) -> fmt::Result {
            write!(f, "{}", self.msg)
        }
    }
    impl StdError for TestError {
        fn source(&self) -> Option<&(dyn StdError + 'static)> {
            match &self.inner {
                Some(x) => Some(x as &dyn StdError),
                None => None,
            }
        }
    }

    #[test]
    fn test_output_levels() {
        initialize();

        // Test standard output
        assert_eq!("wrapped", format!("{}", Error::wrapr(TestError { msg: "cause".to_string(), inner: None }, "wrapped")));

        // Test alternate standard output
        assert_eq!(" error: wrapped\n cause: cause", format!("{:#}", Error::wrapr(TestError { msg: "cause".to_string(), inner: None }, "wrapped")));

        let err = Error::wrapr(TestError { msg: "cause".to_string(), inner: None }, "wrapped");
        assert_eq!(
            " error: witcher::Error: wrapped\n cause: witcher::error::tests::TestError: cause\n",
            format!("{:?}", err).split("symbol").next().unwrap()
        );
        let err = Error::wrapr(
            TestError { msg: "cause".to_string(), inner: Some(Box::new(TestError { msg: "cause2".to_string(), inner: None })) },
            "wrapped",
        );
        assert_eq!(
            " error: witcher::Error: wrapped\n cause: witcher::error::tests::TestError: cause\n cause: std::error::Error: cause2\n",
            format!("{:#?}", err).split("symbol").next().unwrap()
        );
    }

    #[test]
    fn test_chained_cause() {
        initialize();
        let err = TestError {
            msg: "cause 1".to_string(),
            inner: Some(Box::new(TestError {
                msg: "cause 2".to_string(),
                inner: Some(Box::new(TestError { msg: "cause 3".to_string(), inner: None })),
            })),
        };

        assert_eq!(" error: wrapped\n cause: cause 1\n cause: cause 2\n cause: cause 3", format!("{:#}", Error::wrapr(err, "wrapped")));
    }

    #[test]
    fn test_ext_and_last() {
        initialize();
        let err = TestError {
            msg: "cause 1".to_string(),
            inner: Some(Box::new(TestError {
                msg: "cause 2".to_string(),
                inner: Some(Box::new(TestError { msg: "cause 3".to_string(), inner: None })),
            })),
        };
        assert_eq!("foo", Error::wrapr(TestError { msg: "cause 1".to_string(), inner: None }, "foo").to_string());
        assert_eq!("cause 1", Error::wrapr(TestError { msg: "cause 1".to_string(), inner: None }, "foo").ext().to_string());
        assert_eq!("cause 3", Error::wrapr(err, "foo").last().to_string());
    }

    #[test]
    fn test_assist_methods() {
        initialize();
        assert!(Error::raw("").is::<Error>());
        assert!(Error::raw("").downcast_ref::<Error>().is_some());
        assert!(Error::raw("").downcast_mut::<Error>().is_some());
    }
}