#627 - create a custom error message when wasm-bindgen fails because --web isn't available

Change the return type of the child run commands so it can return a CommandError that also captures the stderr if the process fails.
See if the returned output contains the 'Unknown flag: --web' error message and if so provide an alternative error that suggests upgrading
master
Kirk Turner 6 years ago
parent 9423867595
commit 97fdc94d53
  1. 19
      src/bindgen.rs
  2. 94
      src/child.rs

@ -226,10 +226,27 @@ pub fn wasm_bindgen_build(
cmd.arg("--keep-debug"); cmd.arg("--keep-debug");
} }
child::run(cmd, "wasm-bindgen").context("Running the wasm-bindgen CLI")?; let result = child::run(cmd, "wasm-bindgen");
let result: Result<(), failure::Error> = match result {
Ok(r) => Ok(r),
Err(e) => process_error(e),
};
result.context("Running the wasm-bindgen CLI")?;
Ok(()) Ok(())
} }
fn process_error(e: child::CommandError) -> Result<(), failure::Error> {
match &e.stderr {
Some(err) if err.trim().starts_with("Unknown flag: '--web'") =>
bail!("Failed to execute `wasm-bindgen`: --web is not supported. Upgrade wasm-bindgen to a more recent version."),
Some(err) => {
eprintln!("{}", err);
bail!("{}", e.to_string())
},
_ => bail!("{}", e.to_string()),
}
}
/// Check if the `wasm-bindgen` dependency is locally satisfied. /// Check if the `wasm-bindgen` dependency is locally satisfied.
fn wasm_bindgen_version_check(bindgen_path: &PathBuf, dep_version: &str) -> bool { fn wasm_bindgen_version_check(bindgen_path: &PathBuf, dep_version: &str) -> bool {
let mut cmd = Command::new(bindgen_path); let mut cmd = Command::new(bindgen_path);

@ -3,9 +3,9 @@
//! This module helps us ensure that all child processes that we spawn get //! This module helps us ensure that all child processes that we spawn get
//! properly logged and their output is logged as well. //! properly logged and their output is logged as well.
use failure::Error;
use log::info; use log::info;
use std::process::{Command, Stdio}; use std::io::Error as StdError;
use std::process::{Command, ExitStatus, Stdio};
/// Return a new Command object /// Return a new Command object
pub fn new_command(program: &str) -> Command { pub fn new_command(program: &str) -> Command {
@ -23,39 +23,79 @@ pub fn new_command(program: &str) -> Command {
} }
} }
/// Run the given command and return its stdout. /// Error from running Command processes
pub fn run(mut command: Command, command_name: &str) -> Result<(), Error> { /// This captures the standard error output
info!("Running {:?}", command); #[derive(Fail, Debug)]
#[fail(display = "failed to execute `{}`: {}", command_name, fail_reason)]
pub struct CommandError {
command_name: String,
fail_reason: String,
/// the output printed to stderr, if any
pub stderr: Option<String>,
}
let status = command.status()?; impl CommandError {
fn from_status(command_name: &str, status: ExitStatus, stderr: Option<String>) -> CommandError {
CommandError {
command_name: command_name.to_string(),
fail_reason: format!("exited with {}", status),
stderr: stderr,
}
}
if status.success() { fn from_error(command_name: &str, err: StdError) -> CommandError {
Ok(()) CommandError {
} else { command_name: command_name.to_string(),
bail!( fail_reason: err.to_string(),
"failed to execute `{}`: exited with {}", stderr: None,
command_name, }
status
)
} }
} }
/// Run the given command and return its stdout. /// Run the given command and return on success.
pub fn run_capture_stdout(mut command: Command, command_name: &str) -> Result<String, Error> { pub fn run(mut command: Command, command_name: &str) -> Result<(), CommandError> {
info!("Running {:?}", command); info!("Running {:?}", command);
let output = command let cmd_output = command
.stderr(Stdio::inherit())
.stdin(Stdio::inherit()) .stdin(Stdio::inherit())
.output()?; .stdout(Stdio::inherit())
.output();
match cmd_output {
Ok(output) => {
if output.status.success() {
Ok(())
} else {
Err(CommandError::from_status(
command_name,
output.status,
Some(String::from_utf8_lossy(&output.stderr).into_owned()),
))
}
}
Err(e) => Err(CommandError::from_error(command_name, e)),
}
}
if output.status.success() { /// Run the given command and return its stdout.
Ok(String::from_utf8_lossy(&output.stdout).into_owned()) pub fn run_capture_stdout(
} else { mut command: Command,
bail!( command_name: &str,
"failed to execute `{}`: exited with {}", ) -> Result<String, CommandError> {
command_name, info!("Running {:?}", command);
output.status
) let cmd_output = command.stdin(Stdio::inherit()).output();
match cmd_output {
Ok(output) => {
if output.status.success() {
Ok(String::from_utf8_lossy(&output.stdout).into_owned())
} else {
Err(CommandError::from_status(
command_name,
output.status,
Some(String::from_utf8_lossy(&output.stderr).into_owned()),
))
}
}
Err(e) => Err(CommandError::from_error(command_name, e)),
} }
} }

Loading…
Cancel
Save