mtgapi_client/api/
error.rs

1use failure::Backtrace;
2use failure::Context;
3use failure::Fail;
4use std::fmt;
5use std::fmt::Display;
6
7/// Errors encountered by the Client
8#[derive(Debug)]
9pub struct MtgApiError {
10    inner: Context<MtgApiErrorKind>,
11}
12
13#[derive(Clone, Eq, PartialEq, Debug, Fail)]
14pub enum MtgApiErrorKind {
15    #[fail(display = "The Client has been dropped and is no longer available")]
16    ClientDropped,
17    #[fail(display = "Error calling the API Endpoint")]
18    HttpError,
19    #[fail(display = "Failed to read the response body")]
20    BodyReadError,
21    #[fail(display = "Could not parse the response of the card struct")]
22    CardBodyParseError,
23    #[fail(display = "Requested card not found")]
24    CardNotFound,
25    #[fail(display = "Could not parse the response of the set struct")]
26    SetBodyParseError,
27    #[fail(display = "Requested set not found")]
28    SetNotFound,
29    #[fail(display = "Could not parse the response of the types struct")]
30    TypeBodyParseError,
31    #[fail(display = "Could not parse the response of the subtypes struct")]
32    SubtypeBodyParseError,
33    #[fail(display = "Could not parse the response of the supertypes struct")]
34    SupertypeBodyParseError,
35    #[fail(display = "Could not parse the response of the formats struct")]
36    FormatBodyParseError,
37    #[fail(display = "Error: {}", cause)]
38    ApiError { cause: String },
39}
40
41impl Fail for MtgApiError {
42    fn cause(&self) -> Option<&dyn Fail> {
43        self.inner.cause()
44    }
45
46    fn backtrace(&self) -> Option<&Backtrace> {
47        self.inner.backtrace()
48    }
49}
50
51impl Display for MtgApiError {
52    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53        Display::fmt(&self.inner, f)
54    }
55}
56
57impl MtgApiError {
58    pub fn kind(&self) -> MtgApiErrorKind {
59        self.inner.get_context().clone()
60    }
61}
62
63impl From<MtgApiErrorKind> for MtgApiError {
64    fn from(kind: MtgApiErrorKind) -> MtgApiError {
65        MtgApiError {
66            inner: Context::new(kind),
67        }
68    }
69}
70
71impl From<Context<MtgApiErrorKind>> for MtgApiError {
72    fn from(inner: Context<MtgApiErrorKind>) -> MtgApiError {
73        MtgApiError { inner }
74    }
75}