Use anyhow for error handling

Summary:
This is a nice solution if you don't want to spend
too much time thinking about error handling.
You have one common return type and can basically use `?`
everywhere while still maintaining the flexibility to
create custom error types where needed.

Reviewed By: jknoxville

Differential Revision: D21349046

fbshipit-source-id: 073539ce8422cdb3e0141886e95321052bc0c7a3
This commit is contained in:
Pascal Hartig
2020-05-01 09:41:13 -07:00
committed by Facebook GitHub Bot
parent 6f9d82117e
commit 2c20f016d4
4 changed files with 20 additions and 17 deletions

View File

@@ -8,6 +8,7 @@
mod error;
mod types;
use anyhow::{bail, Result};
use clap::value_t_or_exit;
use std::collections::BTreeMap;
use std::fs::File;
@@ -26,7 +27,7 @@ fn pack(
dist_dir: &std::path::PathBuf,
pack_list: &PackList,
output_directory: &std::path::PathBuf,
) -> Result<(), error::Error> {
) -> Result<()> {
let mut frameworks_path = output_directory.clone();
frameworks_path.push("frameworks.tar");
let mut frameworks_tar = tar::Builder::new(File::create(frameworks_path)?);
@@ -64,15 +65,12 @@ fn pack_platform(
pack_list: &PackList,
pack_type: &PackType,
tar_builder: &mut tar::Builder<File>,
) -> Result<(), error::Error> {
) -> Result<()> {
let pack_files = pack_list
.0
.get(platform)
.and_then(|f| f.get(pack_type))
.expect(&format!(
"Missing packlist definition for platform {:?} and pack type {:?}.",
platform, pack_type
));
.ok_or_else(|| error::Error::MissingPackDefinition(platform.clone(), pack_type.clone()))?;
let base_dir = match platform {
Platform::Mac => path::Path::new(dist_dir).join("mac"),
// TODO: Verify this.
@@ -83,7 +81,7 @@ fn pack_platform(
for f in pack_files {
let full_path = path::Path::new(&base_dir).join(f);
if !full_path.exists() {
return Err(error::Error::MissingPackFile(
bail!(error::Error::MissingPackFile(
platform.clone(),
pack_type.clone(),
full_path,