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 66 67 68 69 70 71 72 73
use std; use serde_json; use hyper; #[derive(Debug)] pub enum Error { Hyper(hyper::Error), Io(std::io::Error), Json(serde_json::Error), Client(ErrorKind), } impl std::error::Error for Error { fn description(&self) -> &str { match self { &Error::Hyper(ref e) => e.description(), &Error::Io(ref e) => e.description(), &Error::Json(ref e) => e.description(), &Error::Client(ref e) => match e { &ErrorKind::InvalidQuery => "The query is not valid for the resource", &ErrorKind::ResourceNotFound => "The resource does not exist", &ErrorKind::Api((_, ref message)) => &message, } } } fn cause(&self) -> Option<&std::error::Error> { match self { _ => None, } } } impl std::fmt::Display for Error { fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { write!(f, "{}", std::error::Error::description(self)) } } impl From<hyper::Error> for Error { fn from(err: hyper::Error) -> Error { Error::Hyper(err) } } impl From<std::io::Error> for Error { fn from(err: std::io::Error) -> Error { Error::Io(err) } } impl From<serde_json::Error> for Error { fn from(err: serde_json::Error) -> Error { Error::Json(err) } } impl From<ErrorKind> for Error { fn from(err: ErrorKind) -> Error { Error::Client(err) } } #[derive(Debug)] #[doc(hidden)] pub enum ErrorKind { InvalidQuery, ResourceNotFound, Api((u32, String)) }