Skip to main content

cargo_test_all/
error.rs

1use std::fmt;
2use std::io::Error as IoError;
3use std::result;
4
5use failure::{Backtrace, Context, Fail};
6
7pub type Result<T> = result::Result<T, Error>;
8
9#[derive(Debug)]
10pub struct Error {
11    inner: Context<ErrorKind>,
12}
13
14impl Error {
15    pub fn kind(&self) -> &ErrorKind {
16        self.inner.get_context()
17    }
18}
19
20impl Fail for Error {
21    fn cause(&self) -> Option<&dyn Fail> {
22        self.inner.cause()
23    }
24
25    fn backtrace(&self) -> Option<&Backtrace> {
26        self.inner.backtrace()
27    }
28}
29
30impl fmt::Display for Error {
31    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
32        self.inner.fmt(f)
33    }
34}
35
36#[derive(Clone, Eq, PartialEq, Debug, Fail)]
37pub enum ErrorKind {
38    #[fail(display = "I/O error: {}", reason)]
39    Io { reason: String },
40    #[fail(
41        display = "Passed an invalid UTF-8 value: {:?} at index {}",
42        value, index
43    )]
44    Utf8 { value: Vec<u8>, index: usize },
45    #[fail(display = "Command raised an error: {}", description)]
46    InvalidCommand { description: String },
47    #[fail(
48        display = "Tests for the {} crate are failing. Output: \n{}",
49        crate_name, output
50    )]
51    TestsFailure { crate_name: String, output: String },
52    #[fail(display = "{}", description)]
53    Other { description: String },
54}
55
56impl From<ErrorKind> for Error {
57    fn from(kind: ErrorKind) -> Error {
58        Error::from(Context::new(kind))
59    }
60}
61
62impl From<Context<ErrorKind>> for Error {
63    fn from(inner: Context<ErrorKind>) -> Error {
64        Error { inner }
65    }
66}
67
68impl From<IoError> for Error {
69    fn from(err: IoError) -> Error {
70        Error::from(ErrorKind::Io {
71            reason: format!("{}", err),
72        })
73    }
74}