diff --git a/examples/basic.rs b/examples/basic.rs index ab976b72..0339335e 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -12,13 +12,14 @@ fn index(req: &mut HttpRequest, _payload: Payload, state: &()) -> HttpResponse { } /// handle with path parameters like `/name/{name}/` -fn with_param(req: &mut HttpRequest, _payload: Payload, state: &()) -> HttpResponse { +fn with_param(req: &mut HttpRequest, _payload: Payload, state: &()) + -> HandlerResult +{ println!("{:?}", req); - HttpResponse::builder(StatusCode::OK) - .content_type("test/plain") - .body(format!("Hello {}!", req.match_info().get("name").unwrap())) - .unwrap() + Ok(HttpResponse::builder(StatusCode::OK) + .content_type("test/plain") + .body(format!("Hello {}!", req.match_info().get("name").unwrap()))?) } fn main() { @@ -38,10 +39,10 @@ fn main() { .resource("/", |r| r.handler(Method::GET, |req, _, _| { println!("{:?}", req); - httpcodes::HTTPFound - .builder() - .header("LOCATION", "/index.html") - .body(Body::Empty) + Ok(httpcodes::HTTPFound + .builder() + .header("LOCATION", "/index.html") + .body(Body::Empty)?) })) // static files .route_handler("/static", StaticFiles::new("examples/static/", true))) diff --git a/src/application.rs b/src/application.rs index 2bfa6dba..f3acb869 100644 --- a/src/application.rs +++ b/src/application.rs @@ -180,7 +180,7 @@ impl ApplicationBuilder where S: 'static { /// .resource("/test", |r| { /// r.get::(); /// r.handler(Method::HEAD, |req, payload, state| { - /// httpcodes::HTTPMethodNotAllowed + /// Ok(httpcodes::HTTPMethodNotAllowed) /// }); /// }) /// .finish(); diff --git a/src/error.rs b/src/error.rs index 25223a4e..fb64456d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -123,6 +123,13 @@ impl From for HttpResponse { } } +/// Return `InternalServerError` for `io::Error` +impl From for HttpResponse { + fn from(err: IoError) -> Self { + HttpResponse::from_error(StatusCode::INTERNAL_SERVER_ERROR, err) + } +} + /// Return `BadRequest` for `cookie::ParseError` impl From for HttpResponse { fn from(err: cookie::ParseError) -> Self { diff --git a/src/lib.rs b/src/lib.rs index 75cdddf2..af37f5be 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -52,7 +52,7 @@ pub use httprequest::{HttpRequest, UrlEncoded}; pub use httpresponse::{HttpResponse, HttpResponseBuilder}; pub use payload::{Payload, PayloadItem, PayloadError}; pub use route::{Route, RouteFactory, RouteHandler, RouteResult}; -pub use resource::{Reply, Resource}; +pub use resource::{Reply, Resource, HandlerResult}; pub use recognizer::{Params, RouteRecognizer}; pub use logger::Logger; pub use server::HttpServer; diff --git a/src/resource.rs b/src/resource.rs index 5d55665b..d3a26175 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -15,6 +15,9 @@ use httprequest::HttpRequest; use httpresponse::HttpResponse; use httpcodes::{HTTPNotFound, HTTPMethodNotAllowed}; +/// Result of a resource handler function +pub type HandlerResult = Result; + /// Http resource /// /// `Resource` is an entry in route table which corresponds to requested URL. @@ -48,7 +51,6 @@ impl Default for Resource { } } - impl Resource where S: 'static { pub(crate) fn default_not_found() -> Self { @@ -66,7 +68,7 @@ impl Resource where S: 'static { /// Register handler for specified method. pub fn handler(&mut self, method: Method, handler: F) - where F: Fn(&mut HttpRequest, Payload, &S) -> R + 'static, + where F: Fn(&mut HttpRequest, Payload, &S) -> HandlerResult + 'static, R: Into + 'static, { self.routes.insert(method, Box::new(FnHandler::new(handler))); diff --git a/tests/test_server.rs b/tests/test_server.rs index 66042b21..08557689 100644 --- a/tests/test_server.rs +++ b/tests/test_server.rs @@ -16,7 +16,7 @@ fn create_server() -> HttpServer> { vec![Application::default("/") .resource("/", |r| r.handler(Method::GET, |_, _, _| { - httpcodes::HTTPOk + Ok(httpcodes::HTTPOk) })) .finish()]) } @@ -96,7 +96,7 @@ fn test_middlewares() { finish: act_num3}) .resource("/", |r| r.handler(Method::GET, |_, _, _| { - httpcodes::HTTPOk + Ok(httpcodes::HTTPOk) })) .finish()]) .serve::<_, ()>("127.0.0.1:58904").unwrap();