From 4dd3382ac704251948920aab9f9b66ed6b815dc6 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Wed, 20 Dec 2017 16:13:21 -0800 Subject: [PATCH] update example --- examples/json/src/main.rs | 19 +++++++------------ guide/src/qs_7.md | 8 +++----- 2 files changed, 10 insertions(+), 17 deletions(-) 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 }) } ```