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
use crate::representation::DeviceID;
use failure::{Backtrace, Context, Fail};
use std::error::Error as StdError;
use std::fmt::{Display, Formatter};
use std::result::Result as StdResult;

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

#[derive(Copy, Clone, Eq, PartialEq, Debug, Fail)]
pub enum ContextErrorKind {
    #[fail(display = "Device not connected: {:?}.", device)]
    DeviceNotConnected { device: DeviceID },
    #[fail(display = "Internal connection error.")]
    ConnectionError,
    #[fail(display = "Entity cannot be converted to proto.")]
    EntityIsNone,
}

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

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

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

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

impl From<Context<ContextErrorKind>> for ContextError {
    fn from(inner: Context<ContextErrorKind>) -> ContextError {
        ContextError { inner }
    }
}

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

#[derive(Clone, Eq, PartialEq, Debug, Fail)]
pub enum ConnectionErrorKind {
    #[fail(display = "Sending gRPC request failed.")]
    GRPCSendError,
    #[fail(display = "Error when processing device config file.")]
    DeviceConfigFileError,
    #[fail(display = "Build request from pipeconf failed: {}.", _0)]
    PipeconfError(String),
}

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

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

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

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

impl From<Context<ConnectionErrorKind>> for ConnectionError {
    fn from(inner: Context<ConnectionErrorKind>) -> ConnectionError {
        ConnectionError { inner }
    }
}