From 2d2036875a7c45080a7fc2e9fbf77930399d2532 Mon Sep 17 00:00:00 2001 From: Niel Drummond Date: Sat, 18 Jun 2022 10:47:10 +0100 Subject: [PATCH] Deserialize from bytes, add json_data_content feature --- Cargo.toml | 2 ++ src/docker.rs | 14 +++++++------- src/errors.rs | 9 +++++---- src/read.rs | 1 + 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 98dbcdbb..b8c79baa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,8 @@ test_http = [] test_ssl = ["ssl"] # Enable tests specifically for macos test_macos = [] +# Enable JSON payload in deserialization errors +json_data_content = [] # Enable rustls / ssl ssl = ["dirs-next", "hyper-rustls", "rustls", "rustls-native-certs", "rustls-pemfile", "webpki", "webpki-roots"] ct_logs = ["ssl", "ct-logs"] diff --git a/src/docker.rs b/src/docker.rs index fa4694cc..7319811e 100644 --- a/src/docker.rs +++ b/src/docker.rs @@ -1,6 +1,3 @@ -use std::cmp; -use std::env; -use std::fmt; #[cfg(feature = "ssl")] use std::fs; use std::future::Future; @@ -14,6 +11,7 @@ use std::sync::Arc; use std::time::Duration; #[cfg(feature = "ct_logs")] use std::time::SystemTime; +use std::{cmp, env, fmt}; use futures_core::Stream; use futures_util::future::FutureExt; @@ -1145,15 +1143,17 @@ impl Docker { where T: DeserializeOwned, { - let contents = Docker::decode_into_string(response).await?; + let bytes = hyper::body::to_bytes(response.into_body()).await?; - debug!("Decoded into string: {}", &contents); - serde_json::from_str::(&contents).map_err(|e| { + debug!("Decoded into string: {}", &String::from_utf8_lossy(&bytes)); + + serde_json::from_slice::(&bytes).map_err(|e| { if e.is_data() { JsonDataError { message: e.to_string(), column: e.column(), - contents: contents.to_owned(), + #[cfg(feature = "json_data_content")] + contents: String::from_utf8_lossy(&bytes).to_string(), } } else { e.into() diff --git a/src/errors.rs b/src/errors.rs index 7f4c6d7c..ef70a406 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -54,7 +54,8 @@ pub enum Error { JsonDataError { /// Short section of the json close to the error. message: String, - /// Entire JSON payload. + /// Entire JSON payload. This field is toggled with the **json_data_content** feature cargo flag. + #[cfg(feature = "json_data_content")] contents: String, /// Character sequence at error location. column: usize, @@ -65,6 +66,9 @@ pub enum Error { /// The api version returned by the server. api_version: String, }, + /// Error emitted when a request times out. + #[error("Timeout error")] + RequestTimeoutError, /// Error emitted when JSON fails to serialize. #[error(transparent)] JsonSerdeError { @@ -107,9 +111,6 @@ pub enum Error { #[from] err: hyper::Error, }, - /// Error emitted when a request times out. - #[error("Timeout error")] - RequestTimeoutError, /// Error emitted when serde fails to urlencod a struct of options #[error(transparent)] URLEncodedError { diff --git a/src/read.rs b/src/read.rs index 8983158d..97dd3cfb 100644 --- a/src/read.rs +++ b/src/read.rs @@ -115,6 +115,7 @@ fn decode_json_from_slice(slice: &[u8]) -> Result Err(ref e) if e.is_data() => Err(JsonDataError { message: e.to_string(), column: e.column(), + #[cfg(feature = "json_data_content")] contents: String::from_utf8_lossy(slice).to_string(), }), Err(e) if e.is_eof() => Ok(None),