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
use failure::Backtrace;
use failure::Context;
use failure::Fail;
use std::fmt;
use std::fmt::Display;

/// Errors encountered by the Client
#[derive(Debug)]
pub struct MtgApiError {
    inner: Context<MtgApiErrorKind>,
}

#[derive(Clone, Eq, PartialEq, Debug, Fail)]
pub enum MtgApiErrorKind {
    #[fail(display = "The Client has been dropped and is no longer available")]
    ClientDropped,
    #[fail(display = "Error calling the API Endpoint")]
    HttpError,
    #[fail(display = "Failed to read the response body")]
    BodyReadError,
    #[fail(display = "Could not parse the response of the card struct")]
    CardBodyParseError,
    #[fail(display = "Requested card not found")]
    CardNotFound,
    #[fail(display = "Could not parse the response of the set struct")]
    SetBodyParseError,
    #[fail(display = "Requested set not found")]
    SetNotFound,
    #[fail(display = "Could not parse the response of the types struct")]
    TypeBodyParseError,
    #[fail(display = "Could not parse the response of the subtypes struct")]
    SubtypeBodyParseError,
    #[fail(display = "Could not parse the response of the supertypes struct")]
    SupertypeBodyParseError,
    #[fail(display = "Could not parse the response of the formats struct")]
    FormatBodyParseError,
    #[fail(display = "Error: {}", cause)]
    ApiError { cause: String },
}

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

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

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

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

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

impl From<Context<MtgApiErrorKind>> for MtgApiError {
    fn from(inner: Context<MtgApiErrorKind>) -> MtgApiError {
        MtgApiError { inner }
    }
}