Merge pull request #401 from drager/return-failure-error

Return `Result<T, failure::Error>` instead of `Result<T, wasm_pack::error::Error>`
master
Nick Fitzgerald 7 years ago committed by GitHub
commit 60bdb80947
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      src/binaries.rs
  2. 8
      src/bindgen.rs
  3. 6
      src/build.rs
  4. 4
      src/command/utils.rs
  5. 6
      src/installer.rs
  6. 13
      src/lockfile.rs
  7. 4
      src/logger.rs
  8. 16
      src/manifest/mod.rs
  9. 4
      src/readme.rs
  10. 15
      src/test/webdriver.rs

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

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

@ -12,7 +12,7 @@ use std::str;
use PBAR; use PBAR;
/// Ensure that `rustc` is present and that it is >= 1.30.0 /// 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); let msg = format!("{}Checking `rustc` version...", emoji::CRAB);
PBAR.step(step, &msg); PBAR.step(step, &msg);
let local_minor_version = rustc_minor_version(); let local_minor_version = rustc_minor_version();
@ -25,14 +25,14 @@ pub fn check_rustc_version(step: &Step) -> Result<String, Error> {
mv.to_string() mv.to_string()
), ),
local_minor_version: mv.to_string(), local_minor_version: mv.to_string(),
}) }.into())
} else { } else {
Ok(mv.to_string()) Ok(mv.to_string())
} }
}, },
None => Err(Error::RustcMissing { 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(), 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()),
} }
} }

@ -1,7 +1,7 @@
//! Utility functions for commands. //! Utility functions for commands.
use emoji; use emoji;
use error::Error; use failure;
use progressbar::Step; use progressbar::Step;
use std::fs; use std::fs;
use std::io; use std::io;
@ -20,7 +20,7 @@ pub fn set_crate_path(path: Option<PathBuf>) -> io::Result<PathBuf> {
} }
/// Construct our `pkg` directory in the crate. /// 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); let msg = format!("{}Creating a pkg directory...", emoji::FOLDER);
PBAR.step(step, &msg); PBAR.step(step, &msg);
fs::create_dir_all(&out_dir)?; fs::create_dir_all(&out_dir)?;

@ -23,7 +23,7 @@ use std::path::Path;
use std::process; use std::process;
use atty; use atty;
use failure::{Error, ResultExt}; use failure::{self, ResultExt};
use which; use which;
pub fn install() -> ! { pub fn install() -> ! {
@ -47,7 +47,7 @@ pub fn install() -> ! {
process::exit(0); 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 // Find `rustup.exe` in PATH, we'll be using its installation directory as
// our installation directory. // our installation directory.
let rustup = match which::which("rustup") { let rustup = match which::which("rustup") {
@ -85,7 +85,7 @@ fn do_install() -> Result<(), Error> {
Ok(()) 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 the `-f` argument was passed, we can always overwrite everything.
if env::args().any(|arg| arg == "-f") { if env::args().any(|arg| arg == "-f") {
return Ok(()); return Ok(());

@ -23,10 +23,10 @@ struct Package {
impl Lockfile { impl Lockfile {
/// Read the `Cargo.lock` file for the crate at the given path. /// 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 lock_path = get_lockfile_path(crate_path)?;
let lockfile = fs::read_to_string(lock_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`. /// Get the version of `wasm-bindgen` dependency used in the `Cargo.lock`.
@ -36,7 +36,7 @@ impl Lockfile {
/// Like `wasm_bindgen_version`, except it returns an error instead of /// Like `wasm_bindgen_version`, except it returns an error instead of
/// `None`. /// `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(|| { self.wasm_bindgen_version().ok_or_else(|| {
let message = format!( let message = format!(
"Ensure that you have \"{}\" as a dependency in your Cargo.toml file:\n\ "Ensure that you have \"{}\" as a dependency in your Cargo.toml file:\n\
@ -44,7 +44,7 @@ impl Lockfile {
wasm-bindgen = \"0.2\"", wasm-bindgen = \"0.2\"",
style("wasm-bindgen").bold().dim(), style("wasm-bindgen").bold().dim(),
); );
Error::CrateConfig { message } Error::CrateConfig { message }.into()
}) })
} }
@ -63,7 +63,7 @@ impl Lockfile {
/// Given the path to the crate that we are buliding, return a `PathBuf` /// 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. /// 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. // Identify the crate's root directory, or return an error.
let manifest = crate_path.join("Cargo.toml"); let manifest = crate_path.join("Cargo.toml");
let crate_root = cargo_metadata::metadata(Some(&manifest)) let crate_root = cargo_metadata::metadata(Some(&manifest))
@ -77,7 +77,8 @@ fn get_lockfile_path(crate_path: &Path) -> Result<PathBuf, Error> {
if !lockfile_path.is_file() { if !lockfile_path.is_file() {
Err(Error::CrateConfig { Err(Error::CrateConfig {
message: format!("Could not find lockfile at {:?}", lockfile_path), message: format!("Could not find lockfile at {:?}", lockfile_path),
}) }
.into())
} else { } else {
Ok(lockfile_path) Ok(lockfile_path)
} }

@ -1,7 +1,7 @@
//! Logging facilities for `wasm-pack`. //! Logging facilities for `wasm-pack`.
use command::Command; use command::Command;
use error::Error; use failure;
use slog::{Drain, Level, Logger}; use slog::{Drain, Level, Logger};
use slog_async::Async; use slog_async::Async;
use slog_term::{FullFormat, PlainDecorator}; use slog_term::{FullFormat, PlainDecorator};
@ -9,7 +9,7 @@ use std::fs::OpenOptions;
use std::path::PathBuf; use std::path::PathBuf;
/// Create the logger for wasm-pack that will output any info warning or errors we encounter /// 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 log_path = log_file_path(&cmd);
let file = OpenOptions::new() let file = OpenOptions::new()
.create(true) .create(true)

@ -12,6 +12,7 @@ use self::npm::{
}; };
use emoji; use emoji;
use error::Error; use error::Error;
use failure;
use progressbar::Step; use progressbar::Step;
use serde_json; use serde_json;
use toml; use toml;
@ -76,13 +77,14 @@ struct CargoLib {
crate_type: Option<Vec<String>>, 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"); let manifest_path = path.join("Cargo.toml");
if !manifest_path.is_file() { if !manifest_path.is_file() {
return Err(Error::crate_config(&format!( return Err(Error::crate_config(&format!(
"Crate directory is missing a `Cargo.toml` file; is `{}` the wrong directory?", "Crate directory is missing a `Cargo.toml` file; is `{}` the wrong directory?",
path.display() path.display()
))); ))
.into());
} }
let mut cargo_file = File::open(manifest_path)?; let mut cargo_file = File::open(manifest_path)?;
let mut cargo_contents = String::new(); let mut cargo_contents = String::new();
@ -214,7 +216,7 @@ pub fn write_package_json(
disable_dts: bool, disable_dts: bool,
target: &str, target: &str,
step: &Step, step: &Step,
) -> Result<(), Error> { ) -> Result<(), failure::Error> {
let msg = format!("{}Writing a package.json...", emoji::MEMO); let msg = format!("{}Writing a package.json...", emoji::MEMO);
PBAR.step(step, &msg); PBAR.step(step, &msg);
@ -235,19 +237,19 @@ pub fn write_package_json(
} }
/// Get the crate name for the crate at the given path. /// 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) Ok(read_cargo_toml(path)?.package.name)
} }
/// Check that the crate the given path is properly configured. /// 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); let msg = format!("{}Checking crate configuration...", emoji::WRENCH);
PBAR.step(&step, &msg); PBAR.step(&step, &msg);
check_crate_type(path)?; check_crate_type(path)?;
Ok(()) 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| { if read_cargo_toml(path)?.lib.map_or(false, |lib| {
lib.crate_type lib.crate_type
.map_or(false, |types| types.iter().any(|s| s == "cdylib")) .map_or(false, |types| types.iter().any(|s| s == "cdylib"))
@ -259,5 +261,5 @@ fn check_crate_type(path: &Path) -> Result<(), Error> {
Cargo.toml file:\n\n\ Cargo.toml file:\n\n\
[lib]\n\ [lib]\n\
crate-type = [\"cdylib\", \"rlib\"]" crate-type = [\"cdylib\", \"rlib\"]"
)) ).into())
} }

@ -1,6 +1,6 @@
//! Generating `README` files for the packaged wasm. //! Generating `README` files for the packaged wasm.
use error::Error; use failure;
use std::fs; use std::fs;
use std::path::Path; use std::path::Path;
@ -9,7 +9,7 @@ use progressbar::Step;
use PBAR; use PBAR;
/// Copy the crate's README into the `pkg` directory. /// 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!( assert!(
fs::metadata(path).ok().map_or(false, |m| m.is_dir()), fs::metadata(path).ok().map_or(false, |m| m.is_dir()),
"crate directory should exist" "crate directory should exist"

@ -5,6 +5,7 @@ use binaries::{
}; };
use command::build::BuildMode; use command::build::BuildMode;
use error::Error; use error::Error;
use failure;
use slog::Logger; use slog::Logger;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use target; use target;
@ -15,7 +16,7 @@ pub fn get_or_install_chromedriver(
log: &Logger, log: &Logger,
crate_path: &Path, crate_path: &Path,
mode: BuildMode, mode: BuildMode,
) -> Result<PathBuf, Error> { ) -> Result<PathBuf, failure::Error> {
match (mode, bin_path(log, crate_path, "chromedriver")) { match (mode, bin_path(log, crate_path, "chromedriver")) {
(_, Some(path)) => Ok(path), (_, Some(path)) => Ok(path),
(BuildMode::Normal, None) => install_chromedriver(crate_path), (BuildMode::Normal, None) => install_chromedriver(crate_path),
@ -24,7 +25,8 @@ pub fn get_or_install_chromedriver(
"No crate-local `chromedriver` binary found, and could not find a global \ "No crate-local `chromedriver` binary found, and could not find a global \
`chromedriver` on the `$PATH`. Not installing `chromedriver` because of noinstall \ `chromedriver` on the `$PATH`. Not installing `chromedriver` because of noinstall \
mode.", mode.",
)), )
.into()),
} }
} }
@ -52,7 +54,7 @@ fn get_chromedriver_url() -> Result<String, Error> {
} }
/// Download and install a pre-built `chromedriver` binary. /// 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()?; let url = get_chromedriver_url()?;
install_binaries_from_zip_at_url(crate_path, &url, Some("chromedriver"))?; install_binaries_from_zip_at_url(crate_path, &url, Some("chromedriver"))?;
let chromedriver = get_local_chromedriver_path(crate_path); let chromedriver = get_local_chromedriver_path(crate_path);
@ -66,7 +68,7 @@ pub fn get_or_install_geckodriver(
log: &Logger, log: &Logger,
crate_path: &Path, crate_path: &Path,
mode: BuildMode, mode: BuildMode,
) -> Result<PathBuf, Error> { ) -> Result<PathBuf, failure::Error> {
match (mode, bin_path(log, crate_path, "geckodriver")) { match (mode, bin_path(log, crate_path, "geckodriver")) {
(_, Some(path)) => Ok(path), (_, Some(path)) => Ok(path),
(BuildMode::Normal, None) => install_geckodriver(crate_path), (BuildMode::Normal, None) => install_geckodriver(crate_path),
@ -74,7 +76,8 @@ pub fn get_or_install_geckodriver(
(BuildMode::Noinstall, None) => Err(Error::crate_config( (BuildMode::Noinstall, None) => Err(Error::crate_config(
"No crate-local `geckodriver` binary found, and could not find a global `geckodriver` \ "No crate-local `geckodriver` binary found, and could not find a global `geckodriver` \
on the `$PATH`. Not installing `geckodriver` because of noinstall mode.", on the `$PATH`. Not installing `geckodriver` because of noinstall mode.",
)), )
.into()),
} }
} }
@ -107,7 +110,7 @@ fn get_local_geckodriver_path(crate_path: &Path) -> PathBuf {
} }
/// Download and install a pre-built `geckodriver` binary. /// 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()?; let url = get_geckodriver_url()?;
if url.ends_with("tar.gz") { if url.ends_with("tar.gz") {

Loading…
Cancel
Save