Change parse_env error type from Debug to impl std::error::Error

This is the more appropriate trait I think and should work well for
most real world use cases.
This commit is contained in:
Lukas Kalbertodt
2022-11-06 11:23:21 +01:00
parent 1e74330f00
commit e2dded17fa
5 changed files with 50 additions and 37 deletions

View File

@@ -50,6 +50,13 @@ pub(crate) enum ErrorInner {
msg: String,
},
/// When a custom `parse_env` function fails.
EnvParseError {
field: String,
key: String,
err: Box<dyn std::error::Error + Send + Sync>,
},
/// Returned by the [`Source`] impls for `Path` and `PathBuf` if the file
/// extension is not supported by confique or if the corresponding Cargo
/// feature of confique was not enabled.
@@ -71,6 +78,7 @@ impl std::error::Error for Error {
ErrorInner::MissingValue(_) => None,
ErrorInner::EnvNotUnicode { .. } => None,
ErrorInner::EnvDeserialization { .. } => None,
ErrorInner::EnvParseError { err, .. } => Some(&**err),
ErrorInner::UnsupportedFileFormat { .. } => None,
ErrorInner::MissingFileExtension { .. } => None,
ErrorInner::MissingRequiredFile { .. } => None,
@@ -107,6 +115,10 @@ impl fmt::Display for Error {
std::write!(f, "failed to deserialize value `{field}` from \
environment variable `{key}`: {msg}")
}
ErrorInner::EnvParseError { field, key, err } => {
std::write!(f, "failed to parse environment variable `{key}` into \
field `{field}`: {err}")
}
ErrorInner::UnsupportedFileFormat { path } => {
std::write!(f,
"unknown configuration file format/extension: '{}'",