use std::error::Error;
use std::fmt::{Display, Result as FmtResult, Formatter};
use hyper::status::StatusCode;
use hyper;
use serde_json;
#[derive(Debug)]
pub enum APIError {
ExhaustedListing,
HTTPError(StatusCode),
HyperError(hyper::Error),
JSONError(serde_json::Error)
}
impl Display for APIError {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
write!(f, "Error! {}. ({:?})", self.description(), self)
}
}
impl Error for APIError {
fn description(&self) -> &str {
match *self {
APIError::HTTPError(_) => "The API returned a non-success error code",
APIError::HyperError(_) => "An error occurred while processing the HTTP response",
APIError::JSONError(_) => "The JSON sent by Reddit did not match what rawr was expecting",
_ => "This error should not have occurred. Please file a bug"
}
}
}
impl From<hyper::Error> for APIError {
fn from(err: hyper::Error) -> APIError {
APIError::HyperError(err)
}
}
impl From<serde_json::Error> for APIError {
fn from(err: serde_json::Error) -> APIError {
APIError::JSONError(err)
}
}