Skip to content

Commit

Permalink
Merge pull request #240 from fussybeaver/ND-optimise-decode-response
Browse files Browse the repository at this point in the history
Deserialize from bytes, add json_data_content feature
  • Loading branch information
fussybeaver committed Jun 18, 2022
2 parents 5371861 + 2d20368 commit 80da294
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Expand Up @@ -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"]
Expand Down
14 changes: 7 additions & 7 deletions 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;
Expand All @@ -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;
Expand Down Expand Up @@ -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::<T>(&contents).map_err(|e| {
debug!("Decoded into string: {}", &String::from_utf8_lossy(&bytes));

serde_json::from_slice::<T>(&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()
Expand Down
9 changes: 5 additions & 4 deletions src/errors.rs
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/read.rs
Expand Up @@ -115,6 +115,7 @@ fn decode_json_from_slice<T: DeserializeOwned>(slice: &[u8]) -> Result<Option<T>
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),
Expand Down

0 comments on commit 80da294

Please sign in to comment.