diff --git a/lib/src/catcher.rs b/lib/src/catcher.rs index 95e0151b..791a6dd4 100644 --- a/lib/src/catcher.rs +++ b/lib/src/catcher.rs @@ -43,7 +43,7 @@ impl fmt::Display for Catcher { pub mod defaults { use request::Request; - use response::Response; + use response::{StatusCode, Response}; use super::Catcher; use std::collections::HashMap; @@ -55,8 +55,7 @@ pub mod defaults { } pub fn not_found(_request: Request) -> Response { - // FIXME: Need a way to pass in the status code. - Response::new("\ + Response::with_status(StatusCode::NotFound, "\ \ \ 404: Not Found\ @@ -71,8 +70,7 @@ pub mod defaults { } pub fn internal_error(_request: Request) -> Response { - // FIXME: Need a way to pass in the status code. - Response::new("\ + Response::with_status(StatusCode::InternalServerError, "\ \ \ 404: Not Found\ diff --git a/lib/src/response/mod.rs b/lib/src/response/mod.rs index 97178ba8..1bdec868 100644 --- a/lib/src/response/mod.rs +++ b/lib/src/response/mod.rs @@ -1,6 +1,7 @@ mod empty; mod responder; mod redirect; +mod with_status; pub use hyper::server::Response as HyperResponse; pub use hyper::net::Fresh as HyperFresh; @@ -10,6 +11,7 @@ pub use hyper::header; pub use self::responder::Responder; pub use self::empty::Empty; pub use self::redirect::Redirect; +pub use self::with_status::StatusResponse; use std::ops::{Deref, DerefMut}; @@ -20,6 +22,17 @@ impl<'a> Response<'a> { Response(Box::new(body)) } + pub fn with_status(status: StatusCode, body: T) + -> Response<'a> { + Response(Box::new(StatusResponse::new(status, body))) + } + + pub fn with_raw_status(status: u16, body: T) + -> Response<'a> { + let status_code = StatusCode::from_u16(status); + Response(Box::new(StatusResponse::new(status_code, body))) + } + pub fn empty() -> Response<'a> { Response(Box::new(Empty::new(StatusCode::Ok))) } diff --git a/lib/src/response/with_status.rs b/lib/src/response/with_status.rs index e69de29b..a1f2ae5b 100644 --- a/lib/src/response/with_status.rs +++ b/lib/src/response/with_status.rs @@ -0,0 +1,23 @@ +use response::*; + +pub struct StatusResponse { + status: StatusCode, + responder: R +} + +impl StatusResponse { + pub fn new(status: StatusCode, responder: R) -> StatusResponse { + StatusResponse { + status: status, + responder: responder + } + } +} + +impl Responder for StatusResponse { + fn respond<'b>(&mut self, mut res: HyperResponse<'b, HyperFresh>) { + *(res.status_mut()) = self.status; + self.responder.respond(res); + } +} + diff --git a/macros/src/error_decorator.rs b/macros/src/error_decorator.rs index 7e0d10d2..ea82d186 100644 --- a/macros/src/error_decorator.rs +++ b/macros/src/error_decorator.rs @@ -57,12 +57,13 @@ pub fn error_decorator(ecx: &mut ExtCtxt, sp: Span, meta_item: &MetaItem, let fn_name = item.ident; let catch_fn_name = prepend_ident(CATCH_FN_PREFIX, &item.ident); + let catch_code = error_params.code.node; let catch_fn_item = quote_item!(ecx, fn $catch_fn_name<'rocket>(_req: rocket::Request<'rocket>) -> rocket::Response<'rocket> { // TODO: Figure out what type signature of catcher should be. let result = $fn_name(); - rocket::Response::new(result) + rocket::Response::with_raw_status($catch_code, result) } ).unwrap(); @@ -70,7 +71,6 @@ pub fn error_decorator(ecx: &mut ExtCtxt, sp: Span, meta_item: &MetaItem, push(Annotatable::Item(catch_fn_item)); let struct_name = prepend_ident(CATCH_STRUCT_PREFIX, &item.ident); - let catch_code = error_params.code.node; push(Annotatable::Item(quote_item!(ecx, #[allow(non_upper_case_globals)] pub static $struct_name: rocket::StaticCatchInfo = rocket::StaticCatchInfo {