Skip to content

Commit d9e0197

Browse files
authored
Merge pull request #635 from epage/key
fix: Add key tracking for all error types
2 parents cc1ba8b + 7496699 commit d9e0197

File tree

7 files changed

+288
-67
lines changed

7 files changed

+288
-67
lines changed

src/error.rs

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@ pub enum ConfigError {
7878
key: Option<String>,
7979
},
8080

81+
/// Custom message
82+
At {
83+
/// Error being extended with a path
84+
error: Box<ConfigError>,
85+
86+
/// The URI that references the source that the value came from.
87+
/// Example: `/path/to/config.json` or `Environment` or `etcd://localhost`
88+
// TODO: Why is this called Origin but FileParse has a uri field?
89+
origin: Option<String>,
90+
91+
/// The key in the configuration hash of this value (if available where the
92+
/// error is generated).
93+
key: Option<String>,
94+
},
95+
8196
/// Custom message
8297
Message(String),
8398

@@ -130,7 +145,17 @@ impl ConfigError {
130145
key: Some(key.into()),
131146
},
132147

133-
_ => self,
148+
Self::At { origin, error, .. } => Self::At {
149+
error,
150+
origin,
151+
key: Some(key.into()),
152+
},
153+
154+
other => Self::At {
155+
error: Box::new(other),
156+
origin: None,
157+
key: Some(key.into()),
158+
},
134159
}
135160
}
136161

@@ -157,8 +182,17 @@ impl ConfigError {
157182
expected,
158183
key: Some(concat(key)),
159184
},
185+
Self::At { error, origin, key } => Self::At {
186+
error,
187+
origin,
188+
key: Some(concat(key)),
189+
},
160190
Self::NotFound(key) => Self::NotFound(concat(Some(key))),
161-
_ => self,
191+
other => Self::At {
192+
error: Box::new(other),
193+
origin: None,
194+
key: Some(concat(None)),
195+
},
162196
}
163197
}
164198

@@ -217,6 +251,24 @@ impl fmt::Display for ConfigError {
217251
Ok(())
218252
}
219253

254+
ConfigError::At {
255+
ref error,
256+
ref origin,
257+
ref key,
258+
} => {
259+
write!(f, "{error}")?;
260+
261+
if let Some(ref key) = *key {
262+
write!(f, " for key `{key}`")?;
263+
}
264+
265+
if let Some(ref origin) = *origin {
266+
write!(f, " in {origin}")?;
267+
}
268+
269+
Ok(())
270+
}
271+
220272
ConfigError::FileParse { ref cause, ref uri } => {
221273
write!(f, "{cause}")?;
222274

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"place": {
3+
"name": "Torre di Pisa"
4+
}
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"inner": { "value": 42 }
3+
}

0 commit comments

Comments
 (0)