Add ClientError enum
This commit is contained in:
parent
a8c1f7501e
commit
8192d806f7
|
@ -0,0 +1,80 @@
|
|||
use std::error::Error;
|
||||
use std::{fmt, io};
|
||||
|
||||
use hyper;
|
||||
use rustc_serialize::json;
|
||||
use url;
|
||||
|
||||
use client::response::ParseError;
|
||||
use error::OAuth2Error;
|
||||
|
||||
/// Errors that can occur during authorization.
|
||||
#[derive(Debug)]
|
||||
pub enum ClientError {
|
||||
/// IO error.
|
||||
Io(io::Error),
|
||||
/// URL error.
|
||||
Url(url::ParseError),
|
||||
/// Hyper error.
|
||||
Hyper(hyper::Error),
|
||||
/// JSON error.
|
||||
Json(json::ParserError),
|
||||
/// Response parse error.
|
||||
Parse(ParseError),
|
||||
/// OAuth 2.0 error.
|
||||
OAuth2(OAuth2Error),
|
||||
}
|
||||
|
||||
impl fmt::Display for ClientError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||
match *self {
|
||||
ClientError::Io(ref err) => write!(f, "{}", err),
|
||||
ClientError::Url(ref err) => write!(f, "{}", err),
|
||||
ClientError::Hyper(ref err) => write!(f, "{}", err),
|
||||
ClientError::Json(ref err) => write!(f, "{}", err),
|
||||
ClientError::Parse(ref err) => write!(f, "{}", err),
|
||||
ClientError::OAuth2(ref err) => write!(f, "{}", err),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for ClientError {
|
||||
fn description(&self) -> &str {
|
||||
match *self {
|
||||
ClientError::Io(ref err) => err.description(),
|
||||
ClientError::Url(ref err) => err.description(),
|
||||
ClientError::Hyper(ref err) => err.description(),
|
||||
ClientError::Json(ref err) => err.description(),
|
||||
ClientError::Parse(ref err) => err.description(),
|
||||
ClientError::OAuth2(ref err) => err.description(),
|
||||
}
|
||||
}
|
||||
|
||||
fn cause(&self) -> Option<&Error> {
|
||||
match *self {
|
||||
ClientError::Io(ref err) => Some(err),
|
||||
ClientError::Url(ref err) => Some(err),
|
||||
ClientError::Hyper(ref err) => Some(err),
|
||||
ClientError::Json(ref err) => Some(err),
|
||||
ClientError::Parse(ref err) => Some(err),
|
||||
ClientError::OAuth2(ref err) => Some(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_from {
|
||||
($v:path, $t:ty) => {
|
||||
impl From<$t> for ClientError {
|
||||
fn from(err: $t) -> Self {
|
||||
$v(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl_from!(ClientError::Io, io::Error);
|
||||
impl_from!(ClientError::Url, url::ParseError);
|
||||
impl_from!(ClientError::Hyper, hyper::Error);
|
||||
impl_from!(ClientError::Json, json::ParserError);
|
||||
impl_from!(ClientError::Parse, ParseError);
|
||||
impl_from!(ClientError::OAuth2, OAuth2Error);
|
|
@ -10,6 +10,9 @@ use provider::Provider;
|
|||
|
||||
pub mod response;
|
||||
|
||||
pub use self::error::ClientError;
|
||||
mod error;
|
||||
|
||||
/// OAuth 2.0 client.
|
||||
pub struct Client<P: Provider> {
|
||||
http_client: hyper::Client,
|
||||
|
|
Loading…
Reference in New Issue