Gracefully handle error if field was not defined (#2110)

This commit is contained in:
frozolotl 2023-09-12 19:26:35 +10:00 committed by GitHub
parent 3ca4c94870
commit 14d28dbf4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -83,7 +83,7 @@ fn retrieve(
/// Format the query result in the output format.
fn format(elements: Vec<Content>, command: &QueryCommand) -> StrResult<String> {
if command.one && elements.len() != 1 {
bail!("expected exactly one element, found {}", elements.len())
bail!("expected exactly one element, found {}", elements.len());
}
let mapped: Vec<_> = elements
@ -95,7 +95,10 @@ fn format(elements: Vec<Content>, command: &QueryCommand) -> StrResult<String> {
.collect();
if command.one {
serialize(&mapped[0], command.format)
let Some(value) = mapped.get(0) else {
bail!("no such field found for element");
};
serialize(value, command.format)
} else {
serialize(&mapped, command.format)
}