Add error context

Summary: When file writing fails, attach some context in the error message.

Reviewed By: jknoxville

Differential Revision: D21400273

fbshipit-source-id: f7cce5446ec27d5eb38f1902f24c61169dd644ac
This commit is contained in:
Pascal Hartig
2020-05-07 07:20:29 -07:00
committed by Facebook GitHub Bot
parent 5cc9af4b5d
commit 42e080776f

View File

@@ -8,7 +8,7 @@
mod error;
mod types;
use anyhow::{bail, Result};
use anyhow::{bail, Context, Result};
use clap::value_t_or_exit;
use std::collections::BTreeMap;
use std::fs::File;
@@ -50,7 +50,12 @@ fn pack(
);
let _ = stdout().flush();
let output_path = path::Path::new(output_directory).join(format!("{}.tar", pack_type));
let mut tar = tar::Builder::new(File::create(&output_path)?);
let mut tar = tar::Builder::new(File::create(&output_path).with_context(|| {
format!(
"Couldn't open file for writing: {}",
&output_path.to_string_lossy()
)
})?);
// MacOS uses symlinks for bundling multiple framework versions and pointing
// to the "Current" one.
tar.follow_symlinks(false);
@@ -143,6 +148,12 @@ fn main() -> Result<(), anyhow::Error> {
serde_yaml::from_str(&pack_list_str).expect("Failed to deserialize YAML packlist.");
let output_directory =
&path::PathBuf::from(args.value_of("output").expect("argument has default"));
std::fs::create_dir_all(output_directory).with_context(|| {
format!(
"Failed to create output directory '{}'.",
output_directory.to_string_lossy()
)
})?;
let archive_paths = pack(&platform, &dist_dir, &pack_list, output_directory)?;
manifest(&archive_paths, &output_directory)?;
@@ -166,7 +177,8 @@ fn write_manifest(
archive_manifest: &PackManifest,
) -> Result<path::PathBuf> {
let path = path::PathBuf::from(output_directory).join("manifest.json");
let mut file = File::create(&path)?;
let mut file = File::create(&path)
.with_context(|| format!("Failed to write manifest to {}", &path.to_string_lossy()))?;
file.write_all(&serde_json::to_string_pretty(archive_manifest)?.as_bytes())?;
Ok(path)
}