Mercurial > hg
diff rust/hg-core/src/errors.rs @ 46598:bc08c2331f99
rust: Add a `ConfigValueParseError` variant to common errors
Configuration files are parsed into sections of key/value pairs when
they are read, but at that point values are still arbitrary bytes.
Only when a value is accessed by various parts of the code do we know
its expected type and syntax, so values are parsed at that point.
Let’s make a new error type for this latter kind of parsing error,
and add a variant to the common `HgError` so that most code can propagate it
without much boilerplate.
Differential Revision: https://phab.mercurial-scm.org/D10009
author | Simon Sapin <simon.sapin@octobus.net> |
---|---|
date | Tue, 16 Feb 2021 15:22:20 +0100 |
parents | f031fe1c6ede |
children | 1f55cd5b292f |
line wrap: on
line diff
--- a/rust/hg-core/src/errors.rs Tue Feb 16 13:55:31 2021 +0100 +++ b/rust/hg-core/src/errors.rs Tue Feb 16 15:22:20 2021 +0100 @@ -1,7 +1,8 @@ +use crate::config::ConfigValueParseError; use std::fmt; /// Common error cases that can happen in many different APIs -#[derive(Debug)] +#[derive(Debug, derive_more::From)] pub enum HgError { IoError { error: std::io::Error, @@ -29,6 +30,14 @@ /// The given string is a short explanation for users, not intended to be /// machine-readable. Abort(String), + + /// A configuration value is not in the expected syntax. + /// + /// These errors can happen in many places in the code because values are + /// parsed lazily as the file-level parser does not know the expected type + /// and syntax of each value. + #[from] + ConfigValueParseError(ConfigValueParseError), } /// Details about where an I/O error happened @@ -63,6 +72,7 @@ impl fmt::Display for HgError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { + HgError::Abort(explanation) => write!(f, "{}", explanation), HgError::IoError { error, context } => { write!(f, "{}: {}", error, context) } @@ -72,7 +82,25 @@ HgError::UnsupportedFeature(explanation) => { write!(f, "unsupported feature: {}", explanation) } - HgError::Abort(explanation) => explanation.fmt(f), + HgError::ConfigValueParseError(ConfigValueParseError { + origin: _, + line: _, + section, + item, + value, + expected_type, + }) => { + // TODO: add origin and line number information, here and in + // corresponding python code + write!( + f, + "config error: {}.{} is not a {} ('{}')", + String::from_utf8_lossy(section), + String::from_utf8_lossy(item), + expected_type, + String::from_utf8_lossy(value) + ) + } } } }