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

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

#[derive(Copy, Clone, Eq, PartialEq, Debug, Fail)]
pub enum FeedApiErrorKind {
    #[fail(display = "Error reading config file")]
    Config,
    #[fail(display = "Error parsing Url")]
    Url,
    #[fail(display = "Error logging in")]
    Login,
    #[fail(display = "Error during API call")]
    Api,
    #[fail(display = "IO Error")]
    IO,
    #[fail(display = "Error during portal callback")]
    Portal,
    #[fail(display = "Error parsing feed url")]
    ParseFeed,
    #[fail(display = "Feature unsupported by implementation")]
    Unsupported,
    #[fail(display = "Failed to load API secrets")]
    Secret,
    #[fail(display = "Failed to load embeded resource file")]
    Resource,
    #[fail(display = "No valid CA certificate available")]
    TLSCert,
    #[fail(display = "HTTP Basic Auth required/failed")]
    HTTPAuth,
    #[fail(display = "Error en/decrypting a password")]
    Encryption,
    #[fail(display = "Error parsing Json file")]
    Json,
    #[fail(display = "Unknown Error")]
    Unknown,
}

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

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

impl fmt::Display for FeedApiError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(&self.inner, f)
    }
}

impl FeedApiError {
    pub fn kind(&self) -> FeedApiErrorKind {
        *self.inner.get_context()
    }
}

impl From<FeedApiErrorKind> for FeedApiError {
    fn from(kind: FeedApiErrorKind) -> FeedApiError {
        FeedApiError { inner: Context::new(kind) }
    }
}

impl From<Context<FeedApiErrorKind>> for FeedApiError {
    fn from(inner: Context<FeedApiErrorKind>) -> FeedApiError {
        FeedApiError { inner }
    }
}

impl From<Error> for FeedApiError {
    fn from(_: Error) -> FeedApiError {
        FeedApiError {
            inner: Context::new(FeedApiErrorKind::Unknown),
        }
    }
}