Skip to content

Commit

Permalink
Merge pull request rustwasm#436 from alexcrichton/error
Browse files Browse the repository at this point in the history
Replace internal `Error` with `failure::Error`
  • Loading branch information
alexcrichton authored Nov 6, 2018
2 parents 7796ecb + 11beac3 commit 401f114
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 326 deletions.
39 changes: 12 additions & 27 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

use child;
use emoji;
use error::Error;
use failure::ResultExt;
use failure::{Error, ResultExt};
use progressbar::Step;
use slog::Logger;
use std::path::Path;
Expand All @@ -12,27 +11,22 @@ 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, failure::Error> {
pub fn check_rustc_version(step: &Step) -> Result<String, Error> {
let msg = format!("{}Checking `rustc` version...", emoji::CRAB);
PBAR.step(step, &msg);
let local_minor_version = rustc_minor_version();
match local_minor_version {
Some(mv) => {
if mv < 30 {
return Err(Error::RustcVersion {
message: format!(
"Your version of Rust, '1.{}', is not supported. Please install Rust version 1.30.0 or higher.",
mv.to_string()
),
local_minor_version: mv.to_string(),
}.into())
bail!(
"Your version of Rust, '1.{}', is not supported. Please install Rust version 1.30.0 or higher.",
mv.to_string()
)
} else {
Ok(mv.to_string())
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()),
},
None => bail!("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."),
}
}

Expand All @@ -57,7 +51,7 @@ fn rustc_minor_version() -> Option<u32> {

/// Ensure that `rustup` has the `wasm32-unknown-unknown` target installed for
/// current toolchain
pub fn rustup_add_wasm_target(log: &Logger, step: &Step) -> Result<(), failure::Error> {
pub fn rustup_add_wasm_target(log: &Logger, step: &Step) -> Result<(), Error> {
let msg = format!("{}Adding WASM target...", emoji::TARGET);
PBAR.step(step, &msg);
let mut cmd = Command::new("rustup");
Expand All @@ -68,12 +62,7 @@ pub fn rustup_add_wasm_target(log: &Logger, step: &Step) -> Result<(), failure::
}

/// Run `cargo build` targetting `wasm32-unknown-unknown`.
pub fn cargo_build_wasm(
log: &Logger,
path: &Path,
debug: bool,
step: &Step,
) -> Result<(), failure::Error> {
pub fn cargo_build_wasm(log: &Logger, path: &Path, debug: bool, step: &Step) -> Result<(), Error> {
let msg = format!("{}Compiling to WASM...", emoji::CYCLONE);
PBAR.step(step, &msg);
let mut cmd = Command::new("cargo");
Expand All @@ -87,11 +76,7 @@ pub fn cargo_build_wasm(
}

/// Run `cargo build --tests` targetting `wasm32-unknown-unknown`.
pub fn cargo_build_wasm_tests(
log: &Logger,
path: &Path,
debug: bool,
) -> Result<(), failure::Error> {
pub fn cargo_build_wasm_tests(log: &Logger, path: &Path, debug: bool) -> Result<(), Error> {
let mut cmd = Command::new("cargo");
cmd.current_dir(path).arg("build").arg("--tests");
if !debug {
Expand Down
8 changes: 4 additions & 4 deletions src/child.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
//! This module helps us ensure that all child processes that we spawn get
//! properly logged and their output is logged as well.

use error::Error;
use failure;
use failure::Error;
use slog::Logger;
use std::{
io::{self, Read},
Expand Down Expand Up @@ -120,7 +119,7 @@ pub fn run(
logger: &Logger,
mut command: process::Command,
command_name: &str,
) -> Result<String, failure::Error> {
) -> Result<String, Error> {
info!(logger, "Running {:?}", command);

let mut child = command
Expand Down Expand Up @@ -171,6 +170,7 @@ pub fn run(
if exit.success() {
return Ok(stdout);
} else {
return Err(Error::cli(command_name, stdout.into(), stderr.into(), exit).into());
drop((stdout, stderr));
bail!("failed to execute `{}`: exited with {}", command_name, exit)
}
}
36 changes: 14 additions & 22 deletions src/command/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bindgen;
use build;
use command::utils::{create_pkg_dir, set_crate_path};
use emoji;
use error::Error;
use failure::Error;
use indicatif::HumanDuration;
use lockfile::Lockfile;
use manifest;
Expand Down Expand Up @@ -59,7 +59,7 @@ impl FromStr for BuildMode {
"no-install" => Ok(BuildMode::Noinstall),
"normal" => Ok(BuildMode::Normal),
"force" => Ok(BuildMode::Force),
_ => Err(Error::crate_config(&format!("Unknown build mode: {}", s))),
_ => bail!("Unknown build mode: {}", s),
}
}
}
Expand Down Expand Up @@ -98,11 +98,11 @@ pub struct BuildOptions {
pub out_dir: String,
}

type BuildStep = fn(&mut Build, &Step, &Logger) -> Result<(), failure::Error>;
type BuildStep = fn(&mut Build, &Step, &Logger) -> Result<(), Error>;

impl Build {
/// Construct a build command from the given options.
pub fn try_from_opts(build_opts: BuildOptions) -> Result<Self, failure::Error> {
pub fn try_from_opts(build_opts: BuildOptions) -> Result<Self, Error> {
let crate_path = set_crate_path(build_opts.path)?;
let crate_data = manifest::CrateData::new(&crate_path)?;
let out_dir = crate_path.join(PathBuf::from(build_opts.out_dir));
Expand All @@ -128,7 +128,7 @@ impl Build {
}

/// Execute this `Build` command.
pub fn run(&mut self, log: &Logger) -> Result<(), failure::Error> {
pub fn run(&mut self, log: &Logger) -> Result<(), Error> {
let process_steps = Build::get_process_steps(&self.mode);

let mut step_counter = Step::new(process_steps.len());
Expand Down Expand Up @@ -199,33 +199,29 @@ impl Build {
}
}

fn step_check_rustc_version(
&mut self,
step: &Step,
log: &Logger,
) -> Result<(), failure::Error> {
fn step_check_rustc_version(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
info!(&log, "Checking rustc version...");
let version = build::check_rustc_version(step)?;
let msg = format!("rustc version is {}.", version);
info!(&log, "{}", &msg);
Ok(())
}

fn step_check_crate_config(&mut self, step: &Step, log: &Logger) -> Result<(), failure::Error> {
fn step_check_crate_config(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
info!(&log, "Checking crate configuration...");
self.crate_data.check_crate_config(step)?;
info!(&log, "Crate is correctly configured.");
Ok(())
}

fn step_add_wasm_target(&mut self, step: &Step, log: &Logger) -> Result<(), failure::Error> {
fn step_add_wasm_target(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
info!(&log, "Adding wasm-target...");
build::rustup_add_wasm_target(log, step)?;
info!(&log, "Adding wasm-target was successful.");
Ok(())
}

fn step_build_wasm(&mut self, step: &Step, log: &Logger) -> Result<(), failure::Error> {
fn step_build_wasm(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
info!(&log, "Building wasm...");
build::cargo_build_wasm(log, &self.crate_path, self.debug, step)?;

Expand All @@ -241,14 +237,14 @@ impl Build {
Ok(())
}

fn step_create_dir(&mut self, step: &Step, log: &Logger) -> Result<(), failure::Error> {
fn step_create_dir(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
info!(&log, "Creating a pkg directory...");
create_pkg_dir(&self.out_dir, step)?;
info!(&log, "Created a pkg directory at {:#?}.", &self.crate_path);
Ok(())
}

fn step_create_json(&mut self, step: &Step, log: &Logger) -> Result<(), failure::Error> {
fn step_create_json(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
info!(&log, "Writing a package.json...");
self.crate_data.write_package_json(
&self.out_dir,
Expand All @@ -265,18 +261,14 @@ impl Build {
Ok(())
}

fn step_copy_readme(&mut self, step: &Step, log: &Logger) -> Result<(), failure::Error> {
fn step_copy_readme(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
info!(&log, "Copying readme from crate...");
readme::copy_from_crate(&self.crate_path, &self.out_dir, step)?;
info!(&log, "Copied readme from crate to {:#?}.", &self.out_dir);
Ok(())
}

fn step_install_wasm_bindgen(
&mut self,
step: &Step,
log: &Logger,
) -> Result<(), failure::Error> {
fn step_install_wasm_bindgen(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
info!(&log, "Identifying wasm-bindgen dependency...");
let lockfile = Lockfile::new(&self.crate_data)?;
let bindgen_version = lockfile.require_wasm_bindgen()?;
Expand All @@ -298,7 +290,7 @@ impl Build {
Ok(())
}

fn step_run_wasm_bindgen(&mut self, step: &Step, log: &Logger) -> Result<(), failure::Error> {
fn step_run_wasm_bindgen(&mut self, step: &Step, log: &Logger) -> Result<(), Error> {
info!(&log, "Building the wasm bindings...");
bindgen::wasm_bindgen_build(
&self.crate_data,
Expand Down
18 changes: 2 additions & 16 deletions src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ use self::login::login;
use self::pack::pack;
use self::publish::{access::Access, publish};
use self::test::{Test, TestOptions};
use error::Error;
use failure::Error;
use slog::Logger;
use std::path::PathBuf;
use std::result;
use PBAR;

/// The various kinds of commands that `wasm-pack` can execute.
#[derive(Debug, StructOpt)]
Expand Down Expand Up @@ -84,7 +83,7 @@ pub enum Command {
}

/// Run a command with the given logger!
pub fn run_wasm_pack(command: Command, log: &Logger) -> result::Result<(), failure::Error> {
pub fn run_wasm_pack(command: Command, log: &Logger) -> result::Result<(), Error> {
// Run the correct command based off input and store the result of it so that we can clear
// the progress bar then return it
let status = match command {
Expand Down Expand Up @@ -125,19 +124,6 @@ pub fn run_wasm_pack(command: Command, log: &Logger) -> result::Result<(), failu
}
};

match status {
Ok(_) => {}
Err(ref e) => {
error!(&log, "{}", e);
for c in e.iter_chain() {
if let Some(e) = c.downcast_ref::<Error>() {
PBAR.error(e.error_type());
break;
}
}
}
}

// Return the actual status of the program to the main function
status
}
13 changes: 7 additions & 6 deletions src/command/pack.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use command::utils::{find_pkg_directory, set_crate_path};
use error::Error;
use failure::Error;
use npm;
use slog::Logger;
use std::path::PathBuf;
Expand All @@ -8,15 +8,16 @@ use PBAR;

/// Executes the 'npm pack' command on the 'pkg' directory
/// which creates a tarball that can be published to the NPM registry
pub fn pack(path: Option<PathBuf>, log: &Logger) -> result::Result<(), failure::Error> {
pub fn pack(path: Option<PathBuf>, log: &Logger) -> result::Result<(), Error> {
let crate_path = set_crate_path(path)?;

info!(&log, "Packing up the npm package...");
let pkg_directory = find_pkg_directory(&crate_path).ok_or(Error::PkgNotFound {
message: format!(
let pkg_directory = find_pkg_directory(&crate_path).ok_or_else(|| {
format_err!(
"Unable to find the pkg directory at path {:#?}, or in a child directory of {:#?}",
&crate_path, &crate_path
),
&crate_path,
&crate_path
)
})?;
npm::npm_pack(log, &pkg_directory.to_string_lossy())?;
info!(
Expand Down
4 changes: 2 additions & 2 deletions src/command/publish/access.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use error::Error;
use failure::Error;
use std::fmt;
use std::str::FromStr;

Expand All @@ -19,7 +19,7 @@ impl FromStr for Access {
"public" => Ok(Access::Public),
"restricted" => Ok(Access::Restricted),
"private" => Ok(Access::Restricted),
_ => Err(Error::Unsupported { message: format!("{} is not a supported access level. See https://docs.npmjs.com/cli/access for more information on npm package access levels.", s)}),
_ => bail!("{} is not a supported access level. See https://docs.npmjs.com/cli/access for more information on npm package access levels.", s),
}
}
}
Expand Down
13 changes: 7 additions & 6 deletions src/command/publish/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub mod access;

use self::access::Access;
use command::utils::{find_pkg_directory, set_crate_path};
use error::Error;
use failure::Error;
use npm;
use slog::Logger;
use std::path::PathBuf;
Expand All @@ -16,16 +16,17 @@ pub fn publish(
path: Option<PathBuf>,
access: Option<Access>,
log: &Logger,
) -> result::Result<(), failure::Error> {
) -> result::Result<(), Error> {
let crate_path = set_crate_path(path)?;

info!(&log, "Publishing the npm package...");
info!(&log, "npm info located in the npm debug log");
let pkg_directory = find_pkg_directory(&crate_path).ok_or(Error::PkgNotFound {
message: format!(
let pkg_directory = find_pkg_directory(&crate_path).ok_or_else(|| {
format_err!(
"Unable to find the pkg directory at path '{:#?}', or in a child directory of '{:#?}'",
&crate_path, &crate_path
),
&crate_path,
&crate_path
)
})?;

npm::npm_publish(log, &pkg_directory.to_string_lossy(), access)?;
Expand Down
Loading

0 comments on commit 401f114

Please sign in to comment.