From 7774823e95190d7d4c632a738a017f4056e4cedf Mon Sep 17 00:00:00 2001 From: Techassi Date: Wed, 24 Jan 2024 11:46:48 +0100 Subject: [PATCH] Add context to error (#726) --- src/kvp/mod.rs | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/src/kvp/mod.rs b/src/kvp/mod.rs index 448009658..6e0b82619 100644 --- a/src/kvp/mod.rs +++ b/src/kvp/mod.rs @@ -30,12 +30,12 @@ where { /// Indicates that the key failed to parse. See [`KeyError`] for more /// information about the error causes. - #[snafu(display("failed to parse key of key/value pair"))] - InvalidKey { source: KeyError }, + #[snafu(display("failed to parse key {key:?} of key/value pair"))] + InvalidKey { source: KeyError, key: String }, /// Indicates that the value failed to parse. - #[snafu(display("failed to parse value of key/value pair"))] - InvalidValue { source: E }, + #[snafu(display("failed to parse value {value:?} of key/value pair"))] + InvalidValue { source: E, value: String }, } /// A validated Kubernetes key/value pair. @@ -104,8 +104,13 @@ where type Error = KeyValuePairError; fn try_from(value: (K, V)) -> Result { - let key = Key::from_str(value.0.as_ref()).context(InvalidKeySnafu)?; - let value = T::from_str(value.1.as_ref()).context(InvalidValueSnafu)?; + let key = Key::from_str(value.0.as_ref()).context(InvalidKeySnafu { + key: value.0.as_ref(), + })?; + + let value = T::from_str(value.1.as_ref()).context(InvalidValueSnafu { + value: value.1.as_ref(), + })?; Ok(Self { key, value }) } @@ -385,6 +390,8 @@ pub struct ObjectLabels<'a, T> { #[cfg(test)] mod test { + use snafu::Report; + use super::*; #[test] @@ -462,4 +469,18 @@ mod test { let labels = Labels::try_from_iter(map).unwrap(); assert_eq!(labels.len(), 2); } + + #[test] + fn key_error() { + let err = Label::try_from(("stäckable.tech/vendor", "Stackable")).unwrap_err(); + let report = Report::from_error(err); + println!("{report}") + } + + #[test] + fn value_error() { + let err = Label::try_from(("stackable.tech/vendor", "Stäckable")).unwrap_err(); + let report = Report::from_error(err); + println!("{report}") + } }