1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use std::error::Error as StdError;
use std::fmt::Error as FmtError;
use std::fmt::{Display, Formatter};
use std::io::Error as IoError;
use hyper::error::Error as HyperError;
use hyper::status::StatusCode;
use serde_json::error::Error as JsonError;

#[derive(Debug)]
pub enum Error {
    Io(IoError),
    Json(JsonError),
    Http(HyperError),
    HttpStatus(StatusCode)
}

impl From<IoError> for Error {
    fn from(e: IoError) -> Self {
        Error::Io(e)
    }
}

impl From<JsonError> for Error {
    fn from(e: JsonError) -> Self {
        Error::Json(e)
    }
}

impl From<HyperError> for Error {
    fn from(e: HyperError) -> Self {
        Error::Http(e)
    }
}

impl From<StatusCode> for Error {
    fn from(e: StatusCode) -> Self {
        Error::HttpStatus(e)
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut Formatter) -> Result<(), FmtError> {
        match *self {
            Error::Io(ref e) => Display::fmt(e, f),
            Error::Json(ref e) => Display::fmt(e, f),
            Error::Http(ref e) => Display::fmt(e, f),
            Error::HttpStatus(e) => f.write_fmt(format_args!("Unexpected status code: {}", e))
        }
   }
}

impl StdError for Error {
    fn description(&self) -> &str {
        "HipChat client error"
    }

    fn cause(&self) -> Option<&StdError> {
        match *self {
            Error::Io(ref e) => Some(e),
            Error::Json(ref e) => Some(e),
            Error::Http(ref e) => Some(e),
            Error::HttpStatus(_) => None
        }
    }
}