Skip to main content

rust_eureka/
errors.rs

1use serde_json::error::Error as ParserError;
2use std::error::Error;
3use std::fmt::{self, Display};
4use url::ParseError;
5
6use self::EurekaClientError::*;
7
8/// Errors that can be returned by the [EurekaClient](struct.EurekaClient.html)
9#[derive(Debug)]
10pub enum EurekaClientError {
11    /// An underlying error occurred with the HTTP client
12    ClientError(reqwest::Error),
13    /// An error occurred parsing a response from the server
14    JsonError(ParserError),
15    /// A generic error that was no otherwise typed occurred
16    GenericError(String),
17    /// The Uri of the Eureka server was invalid
18    InvalidUri(ParseError),
19    /// An server error occurred with Eureka
20    InternalServerError,
21    /// Request parameters sent to Eureka were invalid
22    BadRequest,
23    /// The specified resource does not exist in eureka, such as an invalid application name
24    NotFound,
25}
26
27impl Error for EurekaClientError {
28    fn source(&self) -> Option<&(dyn Error + 'static)> {
29        match *self {
30            ClientError(ref error) => Some(error),
31            JsonError(ref error) => Some(error),
32            InvalidUri(ref error) => Some(error),
33            _ => None,
34        }
35    }
36}
37
38impl From<reqwest::Error> for EurekaClientError {
39    fn from(err: reqwest::Error) -> EurekaClientError {
40        ClientError(err)
41    }
42}
43
44impl From<ParserError> for EurekaClientError {
45    fn from(err: ParserError) -> EurekaClientError {
46        JsonError(err)
47    }
48}
49
50impl From<ParseError> for EurekaClientError {
51    fn from(err: ParseError) -> EurekaClientError {
52        InvalidUri(err)
53    }
54}
55
56impl Display for EurekaClientError {
57    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
58        match self {
59            ClientError(e) => write!(f, "HTTP client error: {}", e),
60            JsonError(e) => write!(f, "JSON parsing error: {}", e),
61            GenericError(s) => write!(f, "Generic error: {}", s),
62            InvalidUri(e) => write!(f, "Invalid URI: {}", e),
63            InternalServerError => write!(f, "Internal server error (500)"),
64            BadRequest => write!(f, "Bad request (400)"),
65            NotFound => write!(f, "Not found (404)"),
66        }
67    }
68}