Skip to content

Commit

Permalink
Merge pull request #401 from drager/return-failure-error
Browse files Browse the repository at this point in the history
Return `Result<T, failure::Error>` instead of `Result<T, wasm_pack::error::Error>`
  • Loading branch information
fitzgen authored Oct 9, 2018
2 parents 2ca946e + 9404c14 commit 60bdb80
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 37 deletions.
10 changes: 6 additions & 4 deletions src/binaries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ pub fn install_binaries_from_targz_at_url<'a, I>(
crate_path: &Path,
url: &str,
binaries: I,
) -> Result<(), Error>
) -> Result<(), failure::Error>
where
I: IntoIterator<Item = &'a str>,
{
Expand Down Expand Up @@ -175,7 +175,8 @@ where
.map(|s| s.to_string_lossy())
.collect::<Vec<_>>()
.join(", "),
)))
))
.into())
}
}

Expand All @@ -186,7 +187,7 @@ pub fn install_binaries_from_zip_at_url<'a, I>(
crate_path: &Path,
url: &str,
binaries: I,
) -> Result<(), Error>
) -> Result<(), failure::Error>
where
I: IntoIterator<Item = &'a str>,
{
Expand Down Expand Up @@ -226,7 +227,8 @@ where
.map(|s| s.to_string_lossy())
.collect::<Vec<_>>()
.join(", "),
)))
))
.into())
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/bindgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ pub fn install_wasm_bindgen(
}

/// Download a tarball containing a pre-built `wasm-bindgen` binary.
pub fn download_prebuilt_wasm_bindgen(root_path: &Path, version: &str) -> Result<(), Error> {
pub fn download_prebuilt_wasm_bindgen(
root_path: &Path,
version: &str,
) -> Result<(), failure::Error> {
let target = if target::LINUX && target::x86_64 {
"x86_64-unknown-linux-musl"
} else if target::MACOS && target::x86_64 {
Expand All @@ -65,7 +68,8 @@ pub fn download_prebuilt_wasm_bindgen(root_path: &Path, version: &str) -> Result
} else {
return Err(Error::unsupported(
"there are no pre-built `wasm-bindgen` binaries for this target",
));
)
.into());
};

let url = format!(
Expand Down
6 changes: 3 additions & 3 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::str;
use PBAR;

/// Ensure that `rustc` is present and that it is >= 1.30.0
pub fn check_rustc_version(step: &Step) -> Result<String, Error> {
pub fn check_rustc_version(step: &Step) -> Result<String, failure::Error> {
let msg = format!("{}Checking `rustc` version...", emoji::CRAB);
PBAR.step(step, &msg);
let local_minor_version = rustc_minor_version();
Expand All @@ -25,14 +25,14 @@ pub fn check_rustc_version(step: &Step) -> Result<String, Error> {
mv.to_string()
),
local_minor_version: mv.to_string(),
})
}.into())
} else {
Ok(mv.to_string())
}
},
None => Err(Error::RustcMissing {
message: "We can't figure out what your Rust version is- which means you might not have Rust installed. Please install Rust version 1.30.0 or higher.".to_string(),
}),
}.into()),
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/command/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Utility functions for commands.

use emoji;
use error::Error;
use failure;
use progressbar::Step;
use std::fs;
use std::io;
Expand All @@ -20,7 +20,7 @@ pub fn set_crate_path(path: Option<PathBuf>) -> io::Result<PathBuf> {
}

/// Construct our `pkg` directory in the crate.
pub fn create_pkg_dir(out_dir: &Path, step: &Step) -> Result<(), Error> {
pub fn create_pkg_dir(out_dir: &Path, step: &Step) -> Result<(), failure::Error> {
let msg = format!("{}Creating a pkg directory...", emoji::FOLDER);
PBAR.step(step, &msg);
fs::create_dir_all(&out_dir)?;
Expand Down
6 changes: 3 additions & 3 deletions src/installer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::path::Path;
use std::process;

use atty;
use failure::{Error, ResultExt};
use failure::{self, ResultExt};
use which;

pub fn install() -> ! {
Expand All @@ -47,7 +47,7 @@ pub fn install() -> ! {
process::exit(0);
}

fn do_install() -> Result<(), Error> {
fn do_install() -> Result<(), failure::Error> {
// Find `rustup.exe` in PATH, we'll be using its installation directory as
// our installation directory.
let rustup = match which::which("rustup") {
Expand Down Expand Up @@ -85,7 +85,7 @@ fn do_install() -> Result<(), Error> {
Ok(())
}

fn confirm_can_overwrite(dst: &Path) -> Result<(), Error> {
fn confirm_can_overwrite(dst: &Path) -> Result<(), failure::Error> {
// If the `-f` argument was passed, we can always overwrite everything.
if env::args().any(|arg| arg == "-f") {
return Ok(());
Expand Down
13 changes: 7 additions & 6 deletions src/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ struct Package {

impl Lockfile {
/// Read the `Cargo.lock` file for the crate at the given path.
pub fn new(crate_path: &Path) -> Result<Lockfile, Error> {
pub fn new(crate_path: &Path) -> Result<Lockfile, failure::Error> {
let lock_path = get_lockfile_path(crate_path)?;
let lockfile = fs::read_to_string(lock_path)?;
toml::from_str(&lockfile).map_err(Error::from)
toml::from_str(&lockfile).map_err(|err| Error::from(err).into())
}

/// Get the version of `wasm-bindgen` dependency used in the `Cargo.lock`.
Expand All @@ -36,15 +36,15 @@ impl Lockfile {

/// Like `wasm_bindgen_version`, except it returns an error instead of
/// `None`.
pub fn require_wasm_bindgen(&self) -> Result<&str, Error> {
pub fn require_wasm_bindgen(&self) -> Result<&str, failure::Error> {
self.wasm_bindgen_version().ok_or_else(|| {
let message = format!(
"Ensure that you have \"{}\" as a dependency in your Cargo.toml file:\n\
[dependencies]\n\
wasm-bindgen = \"0.2\"",
style("wasm-bindgen").bold().dim(),
);
Error::CrateConfig { message }
Error::CrateConfig { message }.into()
})
}

Expand All @@ -63,7 +63,7 @@ impl Lockfile {

/// Given the path to the crate that we are buliding, return a `PathBuf`
/// containing the location of the lock file, by finding the workspace root.
fn get_lockfile_path(crate_path: &Path) -> Result<PathBuf, Error> {
fn get_lockfile_path(crate_path: &Path) -> Result<PathBuf, failure::Error> {
// Identify the crate's root directory, or return an error.
let manifest = crate_path.join("Cargo.toml");
let crate_root = cargo_metadata::metadata(Some(&manifest))
Expand All @@ -77,7 +77,8 @@ fn get_lockfile_path(crate_path: &Path) -> Result<PathBuf, Error> {
if !lockfile_path.is_file() {
Err(Error::CrateConfig {
message: format!("Could not find lockfile at {:?}", lockfile_path),
})
}
.into())
} else {
Ok(lockfile_path)
}
Expand Down
4 changes: 2 additions & 2 deletions src/logger.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
//! Logging facilities for `wasm-pack`.

use command::Command;
use error::Error;
use failure;
use slog::{Drain, Level, Logger};
use slog_async::Async;
use slog_term::{FullFormat, PlainDecorator};
use std::fs::OpenOptions;
use std::path::PathBuf;

/// Create the logger for wasm-pack that will output any info warning or errors we encounter
pub fn new(cmd: &Command, verbosity: u8) -> Result<Logger, Error> {
pub fn new(cmd: &Command, verbosity: u8) -> Result<Logger, failure::Error> {
let log_path = log_file_path(&cmd);
let file = OpenOptions::new()
.create(true)
Expand Down
16 changes: 9 additions & 7 deletions src/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use self::npm::{
};
use emoji;
use error::Error;
use failure;
use progressbar::Step;
use serde_json;
use toml;
Expand Down Expand Up @@ -76,13 +77,14 @@ struct CargoLib {
crate_type: Option<Vec<String>>,
}

fn read_cargo_toml(path: &Path) -> Result<CargoManifest, Error> {
fn read_cargo_toml(path: &Path) -> Result<CargoManifest, failure::Error> {
let manifest_path = path.join("Cargo.toml");
if !manifest_path.is_file() {
return Err(Error::crate_config(&format!(
"Crate directory is missing a `Cargo.toml` file; is `{}` the wrong directory?",
path.display()
)));
))
.into());
}
let mut cargo_file = File::open(manifest_path)?;
let mut cargo_contents = String::new();
Expand Down Expand Up @@ -214,7 +216,7 @@ pub fn write_package_json(
disable_dts: bool,
target: &str,
step: &Step,
) -> Result<(), Error> {
) -> Result<(), failure::Error> {
let msg = format!("{}Writing a package.json...", emoji::MEMO);

PBAR.step(step, &msg);
Expand All @@ -235,19 +237,19 @@ pub fn write_package_json(
}

/// Get the crate name for the crate at the given path.
pub fn get_crate_name(path: &Path) -> Result<String, Error> {
pub fn get_crate_name(path: &Path) -> Result<String, failure::Error> {
Ok(read_cargo_toml(path)?.package.name)
}

/// Check that the crate the given path is properly configured.
pub fn check_crate_config(path: &Path, step: &Step) -> Result<(), Error> {
pub fn check_crate_config(path: &Path, step: &Step) -> Result<(), failure::Error> {
let msg = format!("{}Checking crate configuration...", emoji::WRENCH);
PBAR.step(&step, &msg);
check_crate_type(path)?;
Ok(())
}

fn check_crate_type(path: &Path) -> Result<(), Error> {
fn check_crate_type(path: &Path) -> Result<(), failure::Error> {
if read_cargo_toml(path)?.lib.map_or(false, |lib| {
lib.crate_type
.map_or(false, |types| types.iter().any(|s| s == "cdylib"))
Expand All @@ -259,5 +261,5 @@ fn check_crate_type(path: &Path) -> Result<(), Error> {
Cargo.toml file:\n\n\
[lib]\n\
crate-type = [\"cdylib\", \"rlib\"]"
))
).into())
}
4 changes: 2 additions & 2 deletions src/readme.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Generating `README` files for the packaged wasm.

use error::Error;
use failure;
use std::fs;
use std::path::Path;

Expand All @@ -9,7 +9,7 @@ use progressbar::Step;
use PBAR;

/// Copy the crate's README into the `pkg` directory.
pub fn copy_from_crate(path: &Path, out_dir: &Path, step: &Step) -> Result<(), Error> {
pub fn copy_from_crate(path: &Path, out_dir: &Path, step: &Step) -> Result<(), failure::Error> {
assert!(
fs::metadata(path).ok().map_or(false, |m| m.is_dir()),
"crate directory should exist"
Expand Down
15 changes: 9 additions & 6 deletions src/test/webdriver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use binaries::{
};
use command::build::BuildMode;
use error::Error;
use failure;
use slog::Logger;
use std::path::{Path, PathBuf};
use target;
Expand All @@ -15,7 +16,7 @@ pub fn get_or_install_chromedriver(
log: &Logger,
crate_path: &Path,
mode: BuildMode,
) -> Result<PathBuf, Error> {
) -> Result<PathBuf, failure::Error> {
match (mode, bin_path(log, crate_path, "chromedriver")) {
(_, Some(path)) => Ok(path),
(BuildMode::Normal, None) => install_chromedriver(crate_path),
Expand All @@ -24,7 +25,8 @@ pub fn get_or_install_chromedriver(
"No crate-local `chromedriver` binary found, and could not find a global \
`chromedriver` on the `$PATH`. Not installing `chromedriver` because of noinstall \
mode.",
)),
)
.into()),
}
}

Expand Down Expand Up @@ -52,7 +54,7 @@ fn get_chromedriver_url() -> Result<String, Error> {
}

/// Download and install a pre-built `chromedriver` binary.
pub fn install_chromedriver(crate_path: &Path) -> Result<PathBuf, Error> {
pub fn install_chromedriver(crate_path: &Path) -> Result<PathBuf, failure::Error> {
let url = get_chromedriver_url()?;
install_binaries_from_zip_at_url(crate_path, &url, Some("chromedriver"))?;
let chromedriver = get_local_chromedriver_path(crate_path);
Expand All @@ -66,15 +68,16 @@ pub fn get_or_install_geckodriver(
log: &Logger,
crate_path: &Path,
mode: BuildMode,
) -> Result<PathBuf, Error> {
) -> Result<PathBuf, failure::Error> {
match (mode, bin_path(log, crate_path, "geckodriver")) {
(_, Some(path)) => Ok(path),
(BuildMode::Normal, None) => install_geckodriver(crate_path),
(BuildMode::Force, None) => install_geckodriver(crate_path),
(BuildMode::Noinstall, None) => Err(Error::crate_config(
"No crate-local `geckodriver` binary found, and could not find a global `geckodriver` \
on the `$PATH`. Not installing `geckodriver` because of noinstall mode.",
)),
)
.into()),
}
}

Expand Down Expand Up @@ -107,7 +110,7 @@ fn get_local_geckodriver_path(crate_path: &Path) -> PathBuf {
}

/// Download and install a pre-built `geckodriver` binary.
pub fn install_geckodriver(crate_path: &Path) -> Result<PathBuf, Error> {
pub fn install_geckodriver(crate_path: &Path) -> Result<PathBuf, failure::Error> {
let url = get_geckodriver_url()?;

if url.ends_with("tar.gz") {
Expand Down

0 comments on commit 60bdb80

Please sign in to comment.