diff --git a/examples/json/src/main.rs b/examples/json/src/main.rs index 1695c4d7a..f47c195ca 100644 --- a/examples/json/src/main.rs +++ b/examples/json/src/main.rs @@ -6,8 +6,7 @@ extern crate serde_json; #[macro_use] extern crate serde_derive; use actix_web::*; -use futures::Stream; -use futures::future::{Future, ok, err}; +use futures::{Future, Stream}; #[derive(Debug, Serialize, Deserialize)] struct MyObj { @@ -31,16 +30,12 @@ fn index(mut req: HttpRequest) -> Result(&body) { - Ok(obj) => { - println!("model: {:?}", obj); // <- do something with payload - ok(httpcodes::HTTPOk.build() // <- send response - .content_type("application/json") - .json(obj).unwrap()) - }, - Err(e) => err(error::ErrorBadRequest(e).into()) - } - }))) + let obj = serde_json::from_slice::(&body).map_err(error::ErrorBadRequest)?; + + println!("model: {:?}", obj); + Ok(httpcodes::HTTPOk.build().json(obj)?) // <- send response + }) + )) } fn main() { diff --git a/guide/src/qs_7.md b/guide/src/qs_7.md index f821e5e54..2d4368c84 100644 --- a/guide/src/qs_7.md +++ b/guide/src/qs_7.md @@ -85,11 +85,9 @@ fn index(mut req: HttpRequest) -> Future { .from_err() // `Future::and_then` can be used to merge an asynchronous workflow with a // synchronous workflow - .and_then(|body| { // <- body is loaded, now we can deserialize json - let obj = serde_json::from_slice::(&body).unwrap(); - ok(httpcodes::HTTPOk.build() // <- send response - .content_type("application/json") - .json(obj).unwrap()) + .and_then(|body| { // <- body is loaded, now we can deserialize json + let obj = serde_json::from_slice::(&body)?; + Ok(httpcodes::HTTPOk.build().json(obj)?) // <- send response }) } ```