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

Add try_deserialize deserialization path error tracking #632

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ indexmap = { version = "2.2", features = ["serde"], optional = true }
convert_case = { version = "0.6", optional = true }
pathdiff = "0.2"
winnow = "0.6.20"
serde_path_to_error = "0.1"

[dev-dependencies]
serde_derive = "1.0"
Expand Down
16 changes: 15 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::fmt::Debug;

use serde::de::Deserialize;
use serde::ser::Serialize;
use serde_path_to_error::{Deserializer, Track};

use crate::builder::{ConfigBuilder, DefaultState};
use crate::error::{ConfigError, Result};
Expand Down Expand Up @@ -142,7 +143,20 @@ impl Config {

/// Attempt to deserialize the entire configuration into the requested type.
pub fn try_deserialize<'de, T: Deserialize<'de>>(self) -> Result<T> {
T::deserialize(self)
let mut track = Track::new();
match T::deserialize(Deserializer::new(self, &mut track)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also make this change to fn get?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(sorry, just now noticed that this was a whole-document case separate from the path look up case)

Ok(t) => Ok(t),
Err(ConfigError::Message(e)) => {
let path = track.path();
if path.iter().count() == 0 {
return Err(ConfigError::Message(e));
}
Err(ConfigError::Message(format!(
"failed reading field `{path}`: {e}"
)))
}
Err(err) => Err(err),
}
}

/// Attempt to serialize the entire configuration from the given type.
Expand Down
36 changes: 36 additions & 0 deletions tests/testsuite/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,39 @@ fn test_error_root_not_table() {
},
}
}

#[test]
#[cfg(feature = "json")]
fn test_json_error_with_path() {
#[derive(Debug, Deserialize)]
struct InnerSettings {
#[allow(dead_code)]
value: u32,
#[allow(dead_code)]
value2: u32,
}

#[derive(Debug, Deserialize)]
struct Settings {
#[allow(dead_code)]
inner: InnerSettings,
}

let c = Config::builder()
.add_source(File::from_str(
r#"
{
"inner": { "value": 42 }
}
"#,
FileFormat::Json5,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit changes it back to json5. Merge conflict problem?

))
.build()
.unwrap();

let with_path = c.clone().try_deserialize::<Settings>();
assert_data_eq!(
with_path.unwrap_err().to_string(),
str!["failed reading field `inner`: missing field `value2`"]
);
}
2 changes: 1 addition & 1 deletion tests/testsuite/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ fn test_load_level_lowercase() {
assert!(s.is_err());
assert_data_eq!(
s.unwrap_err().to_string(),
str!["enum Level does not have variant constructor error"]
str!["failed reading field `log`: enum Level does not have variant constructor error"]
);
}
Loading