Improve error handling.

This commit is contained in:
r-vdp 2023-04-13 12:08:05 +02:00
parent af853c014c
commit 94119b4ae6
No known key found for this signature in database
2 changed files with 15 additions and 9 deletions

View file

@ -115,15 +115,12 @@ fn try_flake_attr(flake: &str, attr: &str) -> Result<bool> {
fn get_store_path(nix_build_result: process::Output) -> Result<StorePath> {
if nix_build_result.status.success() {
String::from_utf8(nix_build_result.stdout)
.map_err(anyhow::Error::from)
.map_err(|e| anyhow::anyhow!(e).context("Error reading nix build output."))
.and_then(parse_nix_build_output)
} else {
String::from_utf8(nix_build_result.stderr)
.map_err(anyhow::Error::from)
.and_then(|e| {
log::error!("{e}");
anyhow::bail!("Nix build failed.")
})
.and_then(|e| Err(anyhow::anyhow!(e).context("Nix build failed.")))
}
}

View file

@ -183,13 +183,22 @@ fn do_generate(
use_remote_sudo: bool,
) -> Result<()> {
if let Some(target_host) = target_host {
invoke_remote_script(
let status = invoke_remote_script(
&store_path.store_path,
"register-profile",
target_host,
use_remote_sudo,
)?;
Ok(())
if status.success() {
Ok(())
} else {
anyhow::bail!(
"Remote command exited with exit status {}",
status
.code()
.map_or("unknown".to_string(), |c| c.to_string())
)
}
} else {
check_root()?;
system_manager::generate::generate(store_path)
@ -256,10 +265,10 @@ fn do_copy_closure(store_path: &StorePath, target_host: &str) -> Result<()> {
.status()?;
if status.success() {
log::info!("Successfully copied closure to target host");
Ok(())
} else {
log::error!("Error copying closure, {}", status);
anyhow::bail!("Error copying closure, {}", status);
}
Ok(())
}
fn invoke_remote_script(