use elementtree;
use hyper;
use std;
use std::error::Error as StdError;
use std::convert::From;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
Api(ApiErrorResponse),
Http(hyper::Error),
TestOperationInProduction,
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
f.write_str(self.description())
}
}
impl StdError for Error {
fn description(&self) -> &str {
match *self {
Error::Api(ref response) => &response.message,
Error::Http(ref err) => err.description(),
Error::TestOperationInProduction => "Operation not allowed in production environment",
}
}
fn cause(&self) -> Option<&std::error::Error> {
if let Error::Http(ref err) = *self {
return Some(err)
}
None
}
}
impl From<hyper::Error> for Error {
fn from(error: hyper::Error) -> Error {
Error::Http(error)
}
}
impl std::convert::From<Box<std::io::Read>> for Error {
fn from(xml: Box<std::io::Read>) -> Error {
let root = elementtree::Element::from_reader(xml).unwrap();
Error::Api(ApiErrorResponse{
message: String::from(root.find("message").unwrap().text()),
raw: root,
})
}
}
#[derive(Debug)]
pub struct ApiErrorResponse {
pub message: String,
pub raw: elementtree::Element,
}