Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserialize from bytes, add json_data_content feature #240

Merged
merged 1 commit into from Jun 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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