rust_eureka/
errors.rs

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