Skip to content

Commit

Permalink
Add context to error (#726)
Browse files Browse the repository at this point in the history
  • Loading branch information
Techassi authored Jan 24, 2024
1 parent 6bbf707 commit 7774823
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions src/kvp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -104,8 +104,13 @@ where
type Error = KeyValuePairError<T::Error>;

fn try_from(value: (K, V)) -> Result<Self, Self::Error> {
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 })
}
Expand Down Expand Up @@ -385,6 +390,8 @@ pub struct ObjectLabels<'a, T> {

#[cfg(test)]
mod test {
use snafu::Report;

use super::*;

#[test]
Expand Down Expand Up @@ -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}")
}
}

0 comments on commit 7774823

Please sign in to comment.