inth_oauth2_async/client/
error.rs

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