agent/rustjail: Clean up error path in execute_hook()s async task

Clippy (in Rust 1.51 at least) has some complaints about this closure
inside execute_hook() because it uses explicit returns in some places
where it doesn't need them, because they're the last expression in the
function.

That isn't necessarily obvious from a glance, but we can make clippy happy
and also make things a little clearer: first we replace a somewhat verbose
'match' using Option::ok_or_else(), then rearrange the remaining code to
put all the error path first with an explicit return then the "happy" path
as the stright line exit with an implicit return.

fixes #1611

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
This commit is contained in:
David Gibson 2021-04-16 16:41:13 +10:00
parent 6ce1e56d20
commit 75eca6d56f

View File

@ -1528,28 +1528,23 @@ async fn execute_hook(logger: &Logger, h: &Hook, st: &OCIState) -> Result<()> {
match child.wait().await { match child.wait().await {
Ok(exit) => { Ok(exit) => {
let code = match exit.code() { let code = exit
Some(c) => c, .code()
None => { .ok_or_else(|| anyhow!("hook exit status has no status code"))?;
return Err(anyhow!("hook exit status has no status code"));
}
};
if code == 0 { if code != 0 {
debug!(logger, "hook {} exit status is 0", &path);
return Ok(());
} else {
error!(logger, "hook {} exit status is {}", &path, code); error!(logger, "hook {} exit status is {}", &path, code);
return Err(anyhow!(nix::Error::from_errno(Errno::UnknownErrno))); return Err(anyhow!(nix::Error::from_errno(Errno::UnknownErrno)));
} }
debug!(logger, "hook {} exit status is 0", &path);
Ok(())
} }
Err(e) => { Err(e) => Err(anyhow!(
return Err(anyhow!(
"wait child error: {} {}", "wait child error: {} {}",
e, e,
e.raw_os_error().unwrap() e.raw_os_error().unwrap()
)); )),
}
} }
}); });