returning errors with extra fields in config file (#3663)

This commit is contained in:
Tomasz Drwięga 2016-11-29 13:23:28 +01:00 committed by Arkadiy Paronyan
parent 1166013c2b
commit c97c59f62f
3 changed files with 21 additions and 8 deletions

View File

@ -0,0 +1,3 @@
[signer]
passwd = []

View File

@ -649,11 +649,12 @@ mod tests {
fn should_parse_config_and_return_errors() {
let config1 = Args::parse_config(include_str!("./config.invalid1.toml"));
let config2 = Args::parse_config(include_str!("./config.invalid2.toml"));
let config3 = Args::parse_config(include_str!("./config.invalid3.toml"));
match (config1, config2) {
(Err(ArgsError::Parsing(_)), Err(ArgsError::Decode(_))) => {},
(a, b) => {
assert!(false, "Got invalid error types: {:?}, {:?}", a, b);
match (config1, config2, config3) {
(Err(ArgsError::Parsing(_)), Err(ArgsError::Decode(_)), Err(ArgsError::UnknownFields(_))) => {},
(a, b, c) => {
assert!(false, "Got invalid error types: {:?}, {:?}, {:?}", a, b, c);
}
}
}

View File

@ -58,6 +58,7 @@ macro_rules! usage {
Parsing(Vec<toml::ParserError>),
Decode(toml::DecodeError),
Config(String, io::Error),
UnknownFields(String),
}
impl ArgsError {
@ -80,6 +81,11 @@ macro_rules! usage {
println_stderr!("There was an error reading your config file at: {}", path);
println_stderr!("{}", e);
process::exit(2)
},
ArgsError::UnknownFields(fields) => {
println_stderr!("You have some extra fields in your config file:");
println_stderr!("{}", fields);
process::exit(2)
}
}
}
@ -173,10 +179,13 @@ macro_rules! usage {
let mut value_parser = toml::Parser::new(&config);
match value_parser.parse() {
Some(value) => {
let result = rustc_serialize::Decodable::decode(&mut toml::Decoder::new(toml::Value::Table(value)));
match result {
Ok(config) => Ok(config),
Err(e) => Err(e.into()),
let mut decoder = toml::Decoder::new(toml::Value::Table(value));
let result = rustc_serialize::Decodable::decode(&mut decoder);
match (result, decoder.toml) {
(Err(e), _) => Err(e.into()),
(_, Some(toml)) => Err(ArgsError::UnknownFields(toml::encode_str(&toml))),
(Ok(config), None) => Ok(config),
}
},
None => Err(ArgsError::Parsing(value_parser.errors)),