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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use http;
use hyper;
use response;
use serde_json;
use serde_urlencoded;
use std::error;
use std::fmt;
use std::result;

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

/// Represents the errors possible to fall out from Burgundy.
#[derive(Fail, Debug)]
pub enum Error {
    #[fail(display = "Error whilst formatting")]
    FormatError {
        /// The underlying error.
        #[cause]
        error: fmt::Error,
    },

    #[fail(display = "Failed to deserialize response")]
    DeserializationError {
        /// The underlying error.
        #[cause]
        error: serde_json::Error,

        /// The text that was send to Serde. Useful for debugging.
        text: String,
    },

    #[fail(display = "Error serializing the blob into a query {}", error)]
    SerializeQueryError {
        /// The underlying error.
        #[cause]
        error: serde_urlencoded::ser::Error,
    },

    #[fail(display = "Failed to serialize the body for sending {}", error)]
    SerializeBodyError {
        /// The underlying error.
        #[cause]
        error: serde_json::Error,
    },

    #[fail(display = "Http error {}", error)]
    HttpError {
        /// The underlying error.
        #[cause]
        error: http::Error,
    },

    #[fail(display = "Network error {}", error)]
    NetworkError {
        /// The underlying error.
        #[cause]
        error: hyper::Error,
    },

    /// For HTTP requests which do not return 200.
    #[fail(display = "Http request was not ok, status {}", status)]
    RequestNotOk {
        status: u32,
        body: String,
    },
}

impl Error {
    /// Creates a new deserialization error.
    pub(crate) fn new_deserialization_error(
        error: serde_json::Error,
        text: String,
    ) -> Self {
        Error::DeserializationError {
            error,
            text,
        }
    }

    pub(crate) fn new_request_not_ok(response: response::Response) -> Self {
        Error::RequestNotOk {
            status: response.status,
            body: response.body,
        }
    }

    pub(crate) fn new_serialize_query_error(error: serde_urlencoded::ser::Error) -> Self {
        Error::SerializeQueryError {
            error,
        }
    }

    pub(crate) fn new_serialize_body_error(error: serde_json::Error) -> Self {
        Error::SerializeBodyError {
            error,
        }
    }
}

impl From<fmt::Error> for Error {
    fn from(error: fmt::Error) -> Self {
        Error::FormatError {
            error,
        }
    }
}

impl From<http::Error> for Error {
    fn from(error: http::Error) -> Self {
        Error::HttpError {
            error,
        }
    }
}

impl From<hyper::Error> for Error {
    fn from(error: hyper::Error) -> Self {
        Error::NetworkError {
            error,
        }
    }
}

/// Sometimes we have to add an error, even though we know it will never
/// happen.
///
/// This is for those times.
#[derive(Debug)]
pub(crate) struct UnreachableError;

impl error::Error for UnreachableError {
}

impl fmt::Display for UnreachableError {
    fn fmt(
        &self,
        f: &mut fmt::Formatter,
    ) -> fmt::Result {
        write!(f, "This error should never be reached. If you can see it, then please report it as a bug.")
    }
}