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
use failure::{Backtrace, Context, Fail};

#[derive(Debug)]
pub struct Error {
    inner: Context<ErrorKind>,
}

#[derive(Clone, PartialEq, Debug, Fail)]
pub enum ErrorKind {
    #[fail(display = "IO error")]
    Io(::std::io::ErrorKind),
    #[fail(display = "JSON Serialization Error")]
    SerdeJsonSer(::serde_json::error::Category),
    #[fail(display = "JSON Deserialization Error of '{}'", _0)]
    SerdeJsonDe(String),
    #[fail(display = "Interface not found: '{}'", _0)]
    InterfaceNotFound(String),
    #[fail(display = "Invalid parameter: '{}'", _0)]
    InvalidParameter(String),
    #[fail(display = "Method not found: '{}'", _0)]
    MethodNotFound(String),
    #[fail(display = "Method not implemented: '{}'", _0)]
    MethodNotImplemented(String),
    #[fail(display = "Unknown error reply: '{:#?}'", _0)]
    VarlinkErrorReply(::Reply),
    #[fail(display = "Call::reply() called with continues, but without more in the request")]
    CallContinuesMismatch,
    #[fail(display = "Varlink: method called already")]
    MethodCalledAlready,
    #[fail(display = "Varlink: connection busy with other method")]
    ConnectionBusy,
    #[fail(display = "Varlink: Iterator called on old reply")]
    IteratorOldReply,
    #[fail(display = "Server Error")]
    Server,
    #[fail(display = "Timeout Error")]
    Timeout,
    #[fail(display = "Connection Closed")]
    ConnectionClosed,
    #[fail(display = "Invalid varlink address URI")]
    InvalidAddress,
}

impl Fail for Error {
    fn cause(&self) -> Option<&Fail> {
        self.inner.cause()
    }

    fn backtrace(&self) -> Option<&Backtrace> {
        self.inner.backtrace()
    }
}

impl ::std::fmt::Display for Error {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        ::std::fmt::Display::fmt(&self.inner, f)
    }
}

impl Error {
    pub fn kind(&self) -> ErrorKind {
        self.inner.get_context().clone()
    }
}

impl From<ErrorKind> for Error {
    fn from(kind: ErrorKind) -> Error {
        Error {
            inner: Context::new(kind),
        }
    }
}

impl From<Context<ErrorKind>> for Error {
    fn from(inner: Context<ErrorKind>) -> Error {
        Error { inner }
    }
}

impl From<::std::io::Error> for Error {
    fn from(e: ::std::io::Error) -> Error {
        let kind = e.kind();
        e.context(ErrorKind::Io(kind)).into()
    }
}

impl From<::serde_json::Error> for Error {
    fn from(e: ::serde_json::Error) -> Error {
        let category = e.classify();
        e.context(ErrorKind::SerdeJsonSer(category)).into()
    }
}

pub type Result<T> = ::std::result::Result<T, Error>;