Skip to content

Commit

Permalink
error: Make toml get of missing key a silent error, like git config
Browse files Browse the repository at this point in the history
Fixes: #14
  • Loading branch information
gnprice committed Dec 5, 2022
1 parent b138d7c commit 60b7086
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

* `toml get` on a missing key no longer panics. This gives it the same
behavior as `git config`: print nothing, and exit with failure. (#14)
* Fix query parse error on empty quoted key `""`,
as in `toml get data.toml 'foo."".bar'`. (#20)

Expand Down
14 changes: 12 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ enum CliError {
NotArray(),
#[fail(display = "array index out of bounds")]
ArrayIndexOob(),
}

/// An error that should cause a failure exit, but no message on stderr.
#[derive(Debug, Fail)]
enum SilentError {
#[fail(display = "key not found: {}", key)]
KeyNotFound { key: String },
}
Expand All @@ -73,7 +78,12 @@ fn main() {
} => set(&path, &query, &value_str),
};
result.unwrap_or_else(|err| {
eprintln!("toml: {}", err);
match err.downcast::<SilentError>() {
Ok(_) => {}
Err(err) => {
eprintln!("toml: {}", err);
}
}
exit(1);
})
}
Expand All @@ -95,7 +105,7 @@ fn get(path: &PathBuf, query: &str, opts: &GetOpts) -> Result<(), Error> {
}

let item = walk_tpath(doc.as_item(), &tpath);
let item = item.ok_or(CliError::KeyNotFound { key: query.into() })?;
let item = item.ok_or(SilentError::KeyNotFound { key: query.into() })?;

if opts.raw {
if let Item::Value(Value::String(s)) = item {
Expand Down
14 changes: 12 additions & 2 deletions test/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ macro_rules! tomltest_get_err {
};
}

macro_rules! tomltest_get_err_empty {
($name:ident, $args:expr) => {
tomltest!($name, |mut t: TestCaseState| {
t.write_file(INPUT);
t.cmd.args(["get", &t.filename()]).args($args);
check_eq("", &t.expect_error());
});
};
}

macro_rules! tomltest_get {
($name:ident, $args:expr, $expected:expr) => {
tomltest!($name, |mut t: TestCaseState| {
Expand Down Expand Up @@ -91,8 +101,8 @@ tomltest_get!(get_string_raw, ["--raw", "key"], "value\n");
// TODO test `get --output-toml`

tomltest_get_err!(get_invalid_query, [".bad"], "syntax error in query: .bad");
tomltest_get_err!(get_missing, ["nosuchkey"], "key not found");
tomltest_get_err!(get_missing_num, ["key[1]"], "key not found");
tomltest_get_err_empty!(get_missing, ["nosuchkey"]);
tomltest_get_err_empty!(get_missing_num, ["key[1]"]);

macro_rules! tomltest_set {
($name:ident, $args:expr, $expected:expr) => {
Expand Down

0 comments on commit 60b7086

Please sign in to comment.