summaryrefslogtreecommitdiffstats
path: root/src/error.rs
diff options
context:
space:
mode:
authorRyan Leckey <ryan@launchbadge.com>2017-06-03 01:22:24 -0700
committerRyan Leckey <ryan@launchbadge.com>2017-06-03 01:22:24 -0700
commit5eef2539557b9d4eec72c7053457bf3025a3989e (patch)
tree049d7089dc154f39b5d834023703dd5143f68bb0 /src/error.rs
parentfb30d87cf096d0816abc3c40c1ff3f1b8440ee74 (diff)
More context for errors (value key if possible)
Diffstat (limited to 'src/error.rs')
-rw-r--r--src/error.rs33
1 files changed, 28 insertions, 5 deletions
diff --git a/src/error.rs b/src/error.rs
index 8af5cee..92709c2 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -50,12 +50,13 @@ pub enum ConfigError {
origin: Option<String>,
unexpected: Unexpected,
expected: &'static str,
+ key: Option<String>,
},
/// Custom message
Message(String),
- /// Unadorned error from a foreign source.
+ /// Unadorned error from a foreign origin.
Foreign(Box<Error>),
}
@@ -66,9 +67,27 @@ impl ConfigError {
ConfigError::Type {
origin: origin,
unexpected: unexpected,
- expected: expected
+ expected: expected,
+ key: None,
}
}
+
+ // FIXME: pub(crate)
+ #[doc(hidden)]
+ pub fn extend_with_key(self, key: &str) -> Self {
+ match self {
+ ConfigError::Type { origin, unexpected, expected, .. } => {
+ ConfigError::Type {
+ origin: origin,
+ unexpected: unexpected,
+ expected: expected,
+ key: Some(key.into()),
+ }
+ }
+
+ _ => self,
+ }
+ }
}
/// Alias for a `Result` with the error type set to `ConfigError`.
@@ -100,12 +119,16 @@ impl fmt::Display for ConfigError {
write!(f, "configuration property {:?} not found", key)
}
- ConfigError::Type { ref origin, ref unexpected, expected } => {
+ ConfigError::Type { ref origin, ref unexpected, expected, ref key } => {
write!(f, "invalid type: {}, expected {}",
unexpected, expected)?;
+ if let Some(ref key) = *key {
+ write!(f, " for key `{}`", key)?;
+ }
+
if let Some(ref origin) = *origin {
- write!(f, " from {}", origin)?;
+ write!(f, " in {}", origin)?;
}
Ok(())
@@ -115,7 +138,7 @@ impl fmt::Display for ConfigError {
write!(f, "{}", cause)?;
if let Some(ref uri) = *uri {
- write!(f, " from {}", uri)?;
+ write!(f, " in {}", uri)?;
}
Ok(())