aboutsummaryrefslogtreecommitdiff
path: root/src/error.rs
blob: 3e6a66e723f6c056ce013731671cc2ab02109d67 (plain) (blame)
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
use hyper::Error as HyperError;
use serde_json::Error as JsonError;
use serde_urlencoded::ser::Error as UrlEncodeError;
use std::error::Error as StdError;
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::result::Result as StdResult;

/// Result type used throughout the library's public result functions.
pub type Result<T> = StdResult<T, Error>;

/// Standard error enum used to wrap different potential error types.
#[derive(Debug)]
pub enum Error {
    /// An error from the `hyper` crate.
    Hyper(HyperError),
    /// An error from the `serde_json` crate.
    Json(JsonError),
    /// An error from the `serde_urlencoded` crate.
    UrlEncode(UrlEncodeError),
}

impl From<HyperError> for Error {
    fn from(err: HyperError) -> Self {
        Error::Hyper(err)
    }
}

impl From<JsonError> for Error {
    fn from(err: JsonError) -> Self {
        Error::Json(err)
    }
}

impl From<UrlEncodeError> for Error {
    fn from(err: UrlEncodeError) -> Self {
        Error::UrlEncode(err)
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        f.write_str(self.description())
    }
}

impl StdError for Error {
    fn description(&self) -> &str {
        match *self {
            Error::Hyper(ref inner) => inner.description(),
            Error::Json(ref inner) => inner.description(),
            Error::UrlEncode(ref inner) => inner.description(),
        }
    }
}