Make Response::to_json preserve io::Error of ErrorKind::TimedOut

Close #119
This commit is contained in:
Martin Algesten
2020-07-30 17:40:14 +02:00
parent db4cc90956
commit 6614856163
2 changed files with 23 additions and 14 deletions

View File

@@ -400,8 +400,20 @@ impl Response {
/// ```
#[cfg(feature = "json")]
pub fn into_json(self) -> IoResult<serde_json::Value> {
use crate::stream::io_err_timeout;
use std::error::Error;
let reader = self.into_reader();
serde_json::from_reader(reader).map_err(|e| {
// This is to unify TimedOut IoError in the API.
// We make a clone of the original error since serde_json::Error doesn't
// let us get the wrapped error instance back.
if let Some(ioe) = e.source().and_then(|s| s.downcast_ref::<IoError>()) {
if ioe.kind() == ErrorKind::TimedOut {
return io_err_timeout(ioe.to_string());
}
}
IoError::new(
ErrorKind::InvalidData,
format!("Failed to read JSON: {}", e),