use serde_json::error::Error as ParserError;
use std::error::Error;
use std::fmt::{self, Display};
use url::ParseError;
use self::EurekaClientError::*;
#[derive(Debug)]
pub enum EurekaClientError {
ClientError(reqwest::Error),
JsonError(ParserError),
GenericError(String),
InvalidUri(ParseError),
InternalServerError,
BadRequest,
NotFound,
}
impl Error for EurekaClientError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match *self {
ClientError(ref error) => Some(error),
JsonError(ref error) => Some(error),
InvalidUri(ref error) => Some(error),
_ => None,
}
}
}
impl From<reqwest::Error> for EurekaClientError {
fn from(err: reqwest::Error) -> EurekaClientError {
ClientError(err)
}
}
impl From<ParserError> for EurekaClientError {
fn from(err: ParserError) -> EurekaClientError {
JsonError(err)
}
}
impl From<ParseError> for EurekaClientError {
fn from(err: ParseError) -> EurekaClientError {
InvalidUri(err)
}
}
impl Display for EurekaClientError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ClientError(e) => write!(f, "HTTP client error: {}", e),
JsonError(e) => write!(f, "JSON parsing error: {}", e),
GenericError(s) => write!(f, "Generic error: {}", s),
InvalidUri(e) => write!(f, "Invalid URI: {}", e),
InternalServerError => write!(f, "Internal server error (500)"),
BadRequest => write!(f, "Bad request (400)"),
NotFound => write!(f, "Not found (404)"),
}
}
}