extern crate hyper;
extern crate serde_json;
use solicit::http::HttpError;
use url::ParseError;
use serde_json::error as serdeErr;
use hyper::error as hyperErr;
use std::error;
use std::io;
use std::fmt;
use std::result;
use std::string;
use std::str::Utf8Error;
pub type Result<T> = result::Result<T, GithubError>;
pub type Http2Error = HttpError;
#[derive(Debug)]
pub enum GithubError {
JsonParsingSyntax(serdeErr::ErrorCode, usize, usize),
JsonParsingIo(io::Error),
JsonParsingFromUtf8(string::FromUtf8Error),
Request,
RequestMethod,
RequestUri(ParseError),
RequestVersion,
RequestHeader,
RequestTooLarge,
RequestStatus,
RequestIo(io::Error),
RequestSsl(Box<error::Error + Send + Sync>),
RequestHttp2(Http2Error),
RequestUtf8(Utf8Error),
NonJsonBody,
InvalidFields,
Unauthorized,
QueryLimit,
LibIo(io::Error),
LibError,
}
impl fmt::Display for GithubError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
_ => write!(f, "{}", self),
}
}
}
impl error::Error for GithubError {
fn description(&self) -> &str {
use self::GithubError::*;
match *self {
JsonParsingSyntax(..) => "Syntax Error",
JsonParsingIo(ref err) | RequestIo(ref err) => error::Error::description(err),
JsonParsingFromUtf8(..) => "UTF-8 Error",
RequestMethod => "Method in Headers was malformed",
RequestUri(ref err) => error::Error::description(err),
RequestVersion => "Request protocol in Headers was malformed",
RequestHeader => "Headers were malformed",
RequestTooLarge => "Request payload is too big to work",
RequestStatus => "Status Code returned was not a valid code",
RequestSsl(..) => "While performing SSL actions with the request an error occurred",
RequestHttp2(ref err) => error::Error::description(err),
RequestUtf8(ref err) => error::Error::description(err),
Request => "Something went wrong while connecting. We are unsure what the problem is.",
LibIo(..) => "Error occured while performing internal library Io Actions",
NonJsonBody => "JSON was not returned",
InvalidFields => "You've made a request with invalid fields",
Unauthorized => "Your request was unauthorized. Check your OAuth Token.",
QueryLimit => "You've hit your query limit to the API.",
LibError => {
"The github-rs lib has caused an error. Please file a bug report with your \
request, what you expected, and what method you used that failed."
}
}
}
}
impl From<serdeErr::Error> for GithubError {
fn from(serde: serdeErr::Error) -> Self {
use self::GithubError::*;
use serde_json::error::Error::*;
match serde {
Syntax(x, y, z) => JsonParsingSyntax(x, y, z),
Io(err) => JsonParsingIo(err),
FromUtf8(err) => JsonParsingFromUtf8(err),
}
}
}
impl From<hyperErr::Error> for GithubError {
fn from(hyper: hyperErr::Error) -> Self {
use self::GithubError::*;
use hyper::error::Error::*;
match hyper {
Method => RequestMethod,
Uri(err) => RequestUri(err),
Version => RequestVersion,
Header => RequestHeader,
TooLarge => RequestTooLarge,
Status => RequestStatus,
Io(err) => RequestIo(err),
Ssl(err) => RequestSsl(err),
Http2(err) => RequestHttp2(err),
Utf8(err) => RequestUtf8(err),
_ => Request,
}
}
}
macro_rules! try_serde {
($x:expr) => (
match $x {
Ok(x) => Ok(x),
Err(err) => Err(GithubError::from(err)),
}
);
}
macro_rules! try_hyper {
($x:expr) => (
match $x {
Ok(x) => x,
Err(err) => return Err(GithubError::from(err)),
}
);
}