aboutsummaryrefslogtreecommitdiff
path: root/src/error.rs
blob: a1fc9f1f6890ebfa3c4dd72f2ae8a4af7a234d5d (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
55
56
57
58
59
60
61
62
63
64
use hyper::Error as HyperError;
use reqwest::Error as ReqwestError;
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 `reqwest` crate.
    Reqwest(ReqwestError),
    /// 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<ReqwestError> for Error {
    fn from(err: ReqwestError) -> Self {
        Error::Reqwest(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.to_string().as_str())
    }
}

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