impl Error and FromError for HttpError; replace instances of try_io! with try!

This commit is contained in:
Tim Kuehn
2014-11-10 22:59:18 -08:00
parent b9bfdd0537
commit 5605ade5dc
5 changed files with 58 additions and 35 deletions

View File

@@ -145,15 +145,12 @@ pub use status::{Ok, BadRequest, NotFound};
pub use server::Server;
use std::fmt;
use std::error::{Error, FromError};
use std::io::IoError;
use std::rt::backtrace;
macro_rules! try_io(
($e:expr) => (match $e { Ok(v) => v, Err(e) => return Err(::HttpIoError(e)) })
)
macro_rules! todo(
($($arg:tt)*) => (if cfg!(not(ndebug)) {
format_args!(|args| log!(5, "TODO: {}", args), $($arg)*)
@@ -222,6 +219,32 @@ pub enum HttpError {
HttpIoError(IoError),
}
impl Error for HttpError {
fn description(&self) -> &str {
match *self {
HttpMethodError => "Invalid Method specified",
HttpUriError => "Invalid Request URI specified",
HttpVersionError => "Invalid HTTP version specified",
HttpHeaderError => "Invalid Header provided",
HttpStatusError => "Invalid Status provided",
HttpIoError(_) => "An IoError occurred while connecting to the specified network",
}
}
fn cause(&self) -> Option<&Error> {
match *self {
HttpIoError(ref error) => Some(error as &Error),
_ => None,
}
}
}
impl FromError<IoError> for HttpError {
fn from_error(err: IoError) -> HttpError {
HttpIoError(err)
}
}
//FIXME: when Opt-in Built-in Types becomes a thing, we can force these structs
//to be Send. For now, this has the compiler do a static check.
fn _assert_send<T: Send>() {