Skip to main content

other_pocket/
errors.rs

1use hyper::error::Error as HttpError;
2use std::error::Error;
3use std::io::Error as IoError;
4
5#[derive(Debug)]
6pub enum PocketError {
7    Http(HttpError),
8    Json(serde_json::Error),
9    Proto(u16, String),
10    Io(IoError),
11}
12
13impl From<serde_json::Error> for PocketError {
14    fn from(err: serde_json::Error) -> PocketError {
15        PocketError::Json(err)
16    }
17}
18
19impl From<IoError> for PocketError {
20    fn from(err: IoError) -> PocketError {
21        PocketError::Io(err)
22    }
23}
24
25impl From<HttpError> for PocketError {
26    fn from(err: HttpError) -> PocketError {
27        PocketError::Http(err)
28    }
29}
30
31impl Error for PocketError {
32    fn cause(&self) -> Option<&dyn Error> {
33        match *self {
34            PocketError::Http(ref e) => Some(e),
35            PocketError::Json(ref e) => Some(e),
36            PocketError::Proto(..) => None,
37            PocketError::Io(ref e) => Some(e),
38        }
39    }
40}
41
42impl std::fmt::Display for PocketError {
43    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
44        match *self {
45            PocketError::Http(ref e) => e.fmt(fmt),
46            PocketError::Json(ref e) => e.fmt(fmt),
47            PocketError::Proto(ref code, ref msg) => {
48                fmt.write_str(&*format!("{} (code {})", msg, code))
49            }
50            PocketError::Io(ref e) => e.fmt(fmt),
51        }
52    }
53}