From 26fa590d11d7113fbbf8e43dbfc952c0cfa64533 Mon Sep 17 00:00:00 2001 From: Ole Martin Ruud Date: Mon, 29 Sep 2025 11:21:35 +0200 Subject: [PATCH 1/2] feat: Improve error message on missing property --- src/error.rs | 2 +- tests/testsuite/errors.rs | 4 ++-- tests/testsuite/get.rs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/error.rs b/src/error.rs index a63f7c16..d6bed9ba 100644 --- a/src/error.rs +++ b/src/error.rs @@ -230,7 +230,7 @@ impl fmt::Display for ConfigError { ConfigError::Foreign(ref cause) => write!(f, "{cause}"), ConfigError::NotFound(ref key) => { - write!(f, "configuration property {key:?} not found") + write!(f, "missing configuration field {key:?}") } ConfigError::Type { diff --git a/tests/testsuite/errors.rs b/tests/testsuite/errors.rs index 072e54c5..4f044b48 100644 --- a/tests/testsuite/errors.rs +++ b/tests/testsuite/errors.rs @@ -22,7 +22,7 @@ fn test_path_index_bounds() { assert!(res.is_err()); assert_data_eq!( res.unwrap_err().to_string(), - str![[r#"configuration property "arr[2]" not found"#]] + str![[r#"missing configuration field "arr[2]""#]] ); } @@ -45,7 +45,7 @@ fn test_path_index_negative_bounds() { assert!(res.is_err()); assert_data_eq!( res.unwrap_err().to_string(), - str![[r#"configuration property "arr[-1]" not found"#]] + str![[r#"missing configuration field "arr[-1]""#]] ); } diff --git a/tests/testsuite/get.rs b/tests/testsuite/get.rs index 94d30495..c255fc95 100644 --- a/tests/testsuite/get.rs +++ b/tests/testsuite/get.rs @@ -18,7 +18,7 @@ fn test_not_found() { assert!(res.is_err()); assert_data_eq!( res.unwrap_err().to_string(), - str![[r#"configuration property "not_found" not found"#]] + str![[r#"missing configuration field "not_found""#]] ); } From 753165a7b421418e6d104c517d942252ddba6e54 Mon Sep 17 00:00:00 2001 From: Ole Martin Ruud Date: Mon, 29 Sep 2025 11:20:20 +0200 Subject: [PATCH 2/2] feat: Improve error from try_deserialize --- src/error.rs | 4 ++++ tests/testsuite/errors.rs | 8 ++++---- tests/testsuite/file_corn.rs | 2 +- tests/testsuite/file_ini.rs | 2 +- tests/testsuite/file_json.rs | 2 +- tests/testsuite/file_json5.rs | 2 +- tests/testsuite/file_ron.rs | 2 +- tests/testsuite/file_toml.rs | 2 +- tests/testsuite/file_yaml.rs | 2 +- 9 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/error.rs b/src/error.rs index d6bed9ba..bd548dc1 100644 --- a/src/error.rs +++ b/src/error.rs @@ -289,6 +289,10 @@ impl de::Error for ConfigError { fn custom(msg: T) -> Self { Self::Message(msg.to_string()) } + + fn missing_field(field: &'static str) -> Self { + Self::NotFound(field.into()) + } } impl ser::Error for ConfigError { diff --git a/tests/testsuite/errors.rs b/tests/testsuite/errors.rs index 4f044b48..e8a1dbdf 100644 --- a/tests/testsuite/errors.rs +++ b/tests/testsuite/errors.rs @@ -158,7 +158,7 @@ fn test_get_missing_field() { let res = c.get::("inner"); assert_data_eq!( res.unwrap_err().to_string(), - str!["missing field `value2` for key `inner`"] + str![[r#"missing configuration field "value2" for key `inner`"#]] ); } @@ -184,7 +184,7 @@ fn test_get_missing_field_file() { let res = c.get::("inner"); assert_data_eq!( res.unwrap_err().to_string(), - str!["missing field `value2` for key `inner`"] + str![[r#"missing configuration field "value2" for key `inner`"#]] ); } @@ -436,7 +436,7 @@ fn test_deserialize_missing_field() { let res = c.try_deserialize::(); assert_data_eq!( res.unwrap_err().to_string(), - str!["missing field `value2` for key `inner`"] + str![[r#"missing configuration field "inner.value2""#]] ); } @@ -468,6 +468,6 @@ fn test_deserialize_missing_field_file() { let res = c.try_deserialize::(); assert_data_eq!( res.unwrap_err().to_string(), - str!["missing field `value2` for key `inner`"] + str![[r#"missing configuration field "inner.value2""#]] ); } diff --git a/tests/testsuite/file_corn.rs b/tests/testsuite/file_corn.rs index 6ecfd384..be91570d 100644 --- a/tests/testsuite/file_corn.rs +++ b/tests/testsuite/file_corn.rs @@ -185,7 +185,7 @@ fn test_override_uppercase_value_for_struct() { ); } Err(e) => { - if e.to_string().contains("missing field `FOO`") { + if matches!(e, config::ConfigError::NotFound(_)) { assert_eq!( lower_settings.foo, "I HAVE BEEN OVERRIDDEN_WITH_UPPER_CASE".to_owned() diff --git a/tests/testsuite/file_ini.rs b/tests/testsuite/file_ini.rs index 91ea9263..59cf2b1b 100644 --- a/tests/testsuite/file_ini.rs +++ b/tests/testsuite/file_ini.rs @@ -128,7 +128,7 @@ rating = 4.5 ); } Err(e) => { - if e.to_string().contains("missing field `FOO`") { + if matches!(e, config::ConfigError::NotFound(_)) { assert_eq!( lower_settings.foo, "I HAVE BEEN OVERRIDDEN_WITH_UPPER_CASE".to_owned() diff --git a/tests/testsuite/file_json.rs b/tests/testsuite/file_json.rs index cc0706a6..90a1483d 100644 --- a/tests/testsuite/file_json.rs +++ b/tests/testsuite/file_json.rs @@ -178,7 +178,7 @@ fn test_override_uppercase_value_for_struct() { ); } Err(e) => { - if e.to_string().contains("missing field `FOO`") { + if matches!(e, config::ConfigError::NotFound(_)) { println!("triggered error {e:?}"); assert_eq!( lower_settings.foo, diff --git a/tests/testsuite/file_json5.rs b/tests/testsuite/file_json5.rs index 475bb28c..b199c390 100644 --- a/tests/testsuite/file_json5.rs +++ b/tests/testsuite/file_json5.rs @@ -187,7 +187,7 @@ fn test_override_uppercase_value_for_struct() { ); } Err(e) => { - if e.to_string().contains("missing field `FOO`") { + if matches!(e, config::ConfigError::NotFound(_)) { assert_eq!( lower_settings.foo, "I HAVE BEEN OVERRIDDEN_WITH_UPPER_CASE".to_owned() diff --git a/tests/testsuite/file_ron.rs b/tests/testsuite/file_ron.rs index 66c40b22..d0e58686 100644 --- a/tests/testsuite/file_ron.rs +++ b/tests/testsuite/file_ron.rs @@ -179,7 +179,7 @@ fn test_override_uppercase_value_for_struct() { ); } Err(e) => { - if e.to_string().contains("missing field `FOO`") { + if matches!(e, config::ConfigError::NotFound(_)) { assert_eq!( lower_settings.foo, "I HAVE BEEN OVERRIDDEN_WITH_UPPER_CASE".to_owned() diff --git a/tests/testsuite/file_toml.rs b/tests/testsuite/file_toml.rs index 98c3c525..70835c64 100644 --- a/tests/testsuite/file_toml.rs +++ b/tests/testsuite/file_toml.rs @@ -266,7 +266,7 @@ down = 1 ); } Err(e) => { - if e.to_string().contains("missing field `FOO`") { + if matches!(e, config::ConfigError::NotFound(_)) { assert_eq!( lower_settings.foo, "I HAVE BEEN OVERRIDDEN_WITH_UPPER_CASE".to_owned() diff --git a/tests/testsuite/file_yaml.rs b/tests/testsuite/file_yaml.rs index f71caa24..caed24fa 100644 --- a/tests/testsuite/file_yaml.rs +++ b/tests/testsuite/file_yaml.rs @@ -210,7 +210,7 @@ bar: I am bar ); } Err(e) => { - if e.to_string().contains("missing field `FOO`") { + if matches!(e, config::ConfigError::NotFound(_)) { println!("triggered error {e:?}"); assert_eq!( lower_settings.foo,