use std::error;
use std::fmt;
use serde_json;
use crate::client;
#[derive(Debug)]
pub enum RouxError {
Status(client::Response),
Network(client::Error),
Parse(serde_json::Error),
Auth(String),
CredentialsNotSet,
OAuthClientRequired,
}
impl From<client::Error> for RouxError {
fn from(e: client::Error) -> Self {
RouxError::Network(e)
}
}
impl From<serde_json::Error> for RouxError {
fn from(e: serde_json::Error) -> Self {
RouxError::Parse(e)
}
}
impl fmt::Display for RouxError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
RouxError::Status(ref err) => write!(f, "Status error: {}", err.status()),
RouxError::Network(ref err) => err.fmt(f),
RouxError::Parse(ref err) => err.fmt(f),
RouxError::Auth(ref err) => write!(f, "Auth error: {}", err),
RouxError::CredentialsNotSet => write!(
f,
"Must set username and password before calling create_client"
),
RouxError::OAuthClientRequired => {
write!(f, "Endpoint requires authentication with OAuth")
}
}
}
}
impl error::Error for RouxError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match *self {
RouxError::Status(_) => None,
RouxError::Auth(_) => None,
RouxError::Network(ref err) => Some(err),
RouxError::Parse(ref err) => Some(err),
RouxError::CredentialsNotSet => None,
RouxError::OAuthClientRequired => None,
}
}
}