Switch from failure to anyhow (#1851)

This commit switches all of `wasm-bindgen` from the `failure` crate to
`anyhow`. The `anyhow` crate should serve all the purposes that we
previously used `failure` for but has a few advantages:

* It's based on the standard `Error` trait rather than a custom `Fail`
  trait, improving ecosystem compatibility.
* We don't need a `#[derive(Fail)]`, which means that's less code to
  compile for `wasm-bindgen`. This notably helps the compile time of
  `web-sys` itself.
* Using `Result<()>` in `fn main` with `anyhow::Error` produces
  human-readable output, so we can use that natively.
This commit is contained in:
Alex Crichton
2019-11-04 11:35:28 -06:00
committed by GitHub
parent 913fdbc3da
commit 935f71afec
37 changed files with 131 additions and 239 deletions

View File

@ -1,6 +1,6 @@
use crate::shell::Shell;
use anyhow::{bail, format_err, Context, Error};
use curl::easy::Easy;
use failure::{bail, format_err, Error, ResultExt};
use log::{debug, warn};
use rouille::url::Url;
use serde::{Deserialize, Serialize};
@ -138,8 +138,8 @@ pub fn run(server: &SocketAddr, shell: &Shell) -> Result<(), Error> {
// We periodically check the page to see if the output contains a known
// string to only be printed when tests have finished running.
//
// TODO: harness failures aren't well handled here, they always force a
// timeout. These sorts of failures could be "you typo'd the path to a
// TODO: harness anyhows aren't well handled here, they always force a
// timeout. These sorts of anyhows could be "you typo'd the path to a
// local script" which is pretty bad to time out for, we should detect
// this on the page and look for such output here, printing diagnostic
// information.

View File

@ -11,11 +11,10 @@
//! For more documentation about this see the `wasm-bindgen-test` crate README
//! and source code.
use failure::{bail, format_err, Error, ResultExt};
use anyhow::{anyhow, bail, Context};
use std::env;
use std::fs;
use std::path::PathBuf;
use std::process;
use std::thread;
use wasm_bindgen_cli_support::Bindgen;
@ -28,20 +27,8 @@ mod node;
mod server;
mod shell;
fn main() {
fn main() -> anyhow::Result<()> {
env_logger::init();
let err = match rmain() {
Ok(()) => return,
Err(e) => e,
};
eprintln!("error: {}", err);
for cause in err.iter_causes() {
eprintln!("\tcaused by: {}", cause);
}
process::exit(1);
}
fn rmain() -> Result<(), Error> {
let mut args = env::args_os().skip(1);
let shell = shell::Shell::new();
@ -59,7 +46,7 @@ fn rmain() -> Result<(), Error> {
.and_then(|p| p.parent()) // chop off `deps`
.and_then(|p| p.parent()) // chop off `debug`
.map(|p| p.join("wbg-tmp"))
.ok_or_else(|| format_err!("file to test doesn't follow the expected Cargo conventions"))?;
.ok_or_else(|| anyhow!("file to test doesn't follow the expected Cargo conventions"))?;
// Make sure there's no stale state from before
drop(fs::remove_dir_all(&tmpdir));

View File

@ -4,7 +4,7 @@ use std::fs;
use std::path::Path;
use std::process::Command;
use failure::{Error, ResultExt};
use anyhow::{Context, Error};
pub fn execute(
module: &str,

View File

@ -3,7 +3,7 @@ use std::fs;
use std::net::SocketAddr;
use std::path::Path;
use failure::{format_err, Error, ResultExt};
use anyhow::{anyhow, Context, Error};
use rouille::{Request, Response, Server};
pub fn spawn(
@ -90,7 +90,7 @@ pub fn spawn(
response.headers.retain(|(k, _)| k != "Cache-Control");
return response;
})
.map_err(|e| format_err!("{}", e))?;
.map_err(|e| anyhow!("{}", e))?;
return Ok(srv);
fn try_asset(request: &Request, dir: &Path) -> Response {

View File

@ -1,5 +1,5 @@
use anyhow::{bail, Error};
use docopt::Docopt;
use failure::{bail, Error};
use serde::Deserialize;
use std::path::PathBuf;
use std::process;
@ -77,10 +77,7 @@ fn main() {
Ok(()) => return,
Err(e) => e,
};
eprintln!("error: {}", err);
for cause in err.iter_causes() {
eprintln!(" caused by: {}", cause);
}
eprintln!("error: {:?}", err);
process::exit(1);
}

View File

@ -1,9 +1,8 @@
use anyhow::{Context, Error};
use docopt::Docopt;
use failure::{Error, ResultExt};
use serde::Deserialize;
use std::fs;
use std::path::PathBuf;
use std::process;
// no need for jemalloc bloat in this binary (and we don't need speed)
#[global_allocator]
@ -39,24 +38,12 @@ struct Args {
arg_input: PathBuf,
}
fn main() {
fn main() -> anyhow::Result<()> {
let args: Args = Docopt::new(USAGE)
.and_then(|d| d.deserialize())
.unwrap_or_else(|e| e.exit());
let err = match rmain(&args) {
Ok(()) => return,
Err(e) => e,
};
eprintln!("error: {}", err);
for cause in err.iter_causes() {
eprintln!("\tcaused by: {}", cause);
}
process::exit(1);
}
fn rmain(args: &Args) -> Result<(), Error> {
let wasm = fs::read(&args.arg_input)
.with_context(|_| format!("failed to read `{}`", args.arg_input.display()))?;
.with_context(|| format!("failed to read `{}`", args.arg_input.display()))?;
let object = wasm_bindgen_cli_support::wasm2es6js::Config::new()
.base64(args.flag_base64)
@ -70,9 +57,9 @@ fn rmain(args: &Args) -> Result<(), Error> {
let (js, wasm) = object.js_and_wasm()?;
write(args, "js", js.as_bytes(), false)?;
write(&args, "js", js.as_bytes(), false)?;
if let Some(wasm) = wasm {
write(args, "wasm", &wasm, false)?;
write(&args, "wasm", &wasm, false)?;
}
Ok(())
}
@ -81,12 +68,12 @@ fn write(args: &Args, extension: &str, contents: &[u8], print_fallback: bool) ->
if let Some(p) = &args.flag_output {
let dst = p.with_extension(extension);
fs::write(&dst, contents)
.with_context(|_| format!("failed to write `{}`", dst.display()))?;
.with_context(|| format!("failed to write `{}`", dst.display()))?;
} else if let Some(p) = &args.flag_out_dir {
let filename = args.arg_input.file_name().unwrap();
let dst = p.join(filename).with_extension(extension);
fs::write(&dst, contents)
.with_context(|_| format!("failed to write `{}`", dst.display()))?;
.with_context(|| format!("failed to write `{}`", dst.display()))?;
} else if print_fallback {
println!("{}", String::from_utf8_lossy(contents))
}