inth_oauth2/client/
error.rs

1use std::error::Error;
2use std::{fmt, io};
3
4use reqwest;
5use serde_json;
6use url;
7
8use client::response::ParseError;
9use error::OAuth2Error;
10
11/// Errors that can occur during authorization.
12#[derive(Debug)]
13pub enum ClientError {
14    /// IO error.
15    Io(io::Error),
16
17    /// URL error.
18    Url(url::ParseError),
19
20    /// Reqwest error.
21    Reqwest(reqwest::Error),
22
23    /// JSON error.
24    Json(serde_json::Error),
25
26    /// Response parse error.
27    Parse(ParseError),
28
29    /// OAuth 2.0 error.
30    OAuth2(OAuth2Error),
31}
32
33impl fmt::Display for ClientError {
34    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
35        match *self {
36            ClientError::Io(ref err) => write!(f, "{}", err),
37            ClientError::Url(ref err) => write!(f, "{}", err),
38            ClientError::Reqwest(ref err) => write!(f, "{}", err),
39            ClientError::Json(ref err) => write!(f, "{}", err),
40            ClientError::Parse(ref err) => write!(f, "{}", err),
41            ClientError::OAuth2(ref err) => write!(f, "{}", err),
42        }
43    }
44}
45
46impl Error for ClientError {
47    fn description(&self) -> &str {
48        match *self {
49            ClientError::Io(ref err) => err.description(),
50            ClientError::Url(ref err) => err.description(),
51            ClientError::Reqwest(ref err) => err.description(),
52            ClientError::Json(ref err) => err.description(),
53            ClientError::Parse(ref err) => err.description(),
54            ClientError::OAuth2(ref err) => err.description(),
55        }
56    }
57
58    fn cause(&self) -> Option<&Error> {
59        match *self {
60            ClientError::Io(ref err) => Some(err),
61            ClientError::Url(ref err) => Some(err),
62            ClientError::Reqwest(ref err) => Some(err),
63            ClientError::Json(ref err) => Some(err),
64            ClientError::Parse(ref err) => Some(err),
65            ClientError::OAuth2(ref err) => Some(err),
66        }
67    }
68}
69
70macro_rules! impl_from {
71    ($v:path, $t:ty) => {
72        impl From<$t> for ClientError {
73            fn from(err: $t) -> Self {
74                $v(err)
75            }
76        }
77    }
78}
79
80impl_from!(ClientError::Io, io::Error);
81impl_from!(ClientError::Url, url::ParseError);
82impl_from!(ClientError::Reqwest, reqwest::Error);
83impl_from!(ClientError::Json, serde_json::Error);
84impl_from!(ClientError::Parse, ParseError);
85impl_from!(ClientError::OAuth2, OAuth2Error);