From 2f1b3b352b3d9d6abc11513a05a45fb7acf7b39e Mon Sep 17 00:00:00 2001 From: Corentin Henry Date: Wed, 31 May 2017 16:41:43 -0700 Subject: [PATCH 1/4] handle json serialization errors fixes https://github.com/seanmonstar/reqwest/issues/112 --- src/client.rs | 8 ++++---- src/lib.rs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/client.rs b/src/client.rs index 319d987..d25e609 100644 --- a/src/client.rs +++ b/src/client.rs @@ -612,16 +612,16 @@ impl RequestBuilder { /// /// let client = reqwest::Client::new()?; /// let res = client.post("http://httpbin.org") - /// .json(&map) + /// .json(&map)? /// .send()?; /// # Ok(()) /// # } /// ``` - pub fn json(mut self, json: &T) -> RequestBuilder { - let body = serde_json::to_vec(json).expect("serde to_vec cannot fail"); + pub fn json(mut self, json: &T) -> ::Result { + let body = serde_json::to_vec(json).map_err(::error::from)?; self.headers.set(ContentType::json()); self.body = Some(Ok(body.into())); - self + Ok(self) } /// Build a `Request`, which can be inspected, modified and executed with diff --git a/src/lib.rs b/src/lib.rs index e9080af..6de09ea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -101,7 +101,7 @@ //! //! let client = reqwest::Client::new()?; //! let res = client.post("http://httpbin.org/post") -//! .json(&map) +//! .json(&map)? //! .send()?; //! # Ok(()) //! # } From 80a80379d35142eadffb6512fc81612d3b676a2c Mon Sep 17 00:00:00 2001 From: Corentin Henry Date: Wed, 31 May 2017 16:42:44 -0700 Subject: [PATCH 2/4] update json tests --- src/client.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/client.rs b/src/client.rs index d25e609..53bbdee 100644 --- a/src/client.rs +++ b/src/client.rs @@ -827,7 +827,7 @@ mod tests { let mut json_data = HashMap::new(); json_data.insert("foo", "bar"); - r = r.json(&json_data); + r = r.json(&json_data).unwrap(); // Make sure the content type was set assert_eq!(r.headers.get::(), Some(&ContentType::json())); @@ -837,4 +837,24 @@ mod tests { let body_should_be = serde_json::to_string(&json_data).unwrap(); assert_eq!(buf, body_should_be); } + + #[test] + fn add_json_fail() { + use serde::{Serialize, Serializer}; + use serde::ser::Error; + struct MyStruct; + impl Serialize for MyStruct { + fn serialize(&self, _serializer: S) -> Result + where S: Serializer + { + Err(S::Error::custom("nope")) + } + } + + let client = Client::new().unwrap(); + let some_url = "https://google.com/"; + let r = client.post(some_url); + let json_data = MyStruct{}; + assert_eq!(format!("{}", r.json(&json_data).unwrap_err()), "nope".to_string()); + } } From 20f2896ab42788a44eac60d243ce957828f223fc Mon Sep 17 00:00:00 2001 From: Corentin Henry Date: Wed, 31 May 2017 17:41:48 -0700 Subject: [PATCH 3/4] tweak add_json_fail test --- src/client.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client.rs b/src/client.rs index 53bbdee..bfa2a96 100644 --- a/src/client.rs +++ b/src/client.rs @@ -855,6 +855,6 @@ mod tests { let some_url = "https://google.com/"; let r = client.post(some_url); let json_data = MyStruct{}; - assert_eq!(format!("{}", r.json(&json_data).unwrap_err()), "nope".to_string()); + assert!(r.json(&json_data).unwrap_err().is_serialization()); } } From 0f0bf882d9668e860272537dd2985707c6c10afe Mon Sep 17 00:00:00 2001 From: Corentin Henry Date: Wed, 31 May 2017 17:50:20 -0700 Subject: [PATCH 4/4] add an error section to `RequestBuilder.json` --- src/client.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/client.rs b/src/client.rs index bfa2a96..7b7cb9f 100644 --- a/src/client.rs +++ b/src/client.rs @@ -617,6 +617,11 @@ impl RequestBuilder { /// # Ok(()) /// # } /// ``` + /// + /// # Errors + /// + /// Serialization can fail if `T`'s implementation of `Serialize` decides to + /// fail, or if `T` contains a map with non-string keys. pub fn json(mut self, json: &T) -> ::Result { let body = serde_json::to_vec(json).map_err(::error::from)?; self.headers.set(ContentType::json());