Migrate from failure to thiserror and anyhow (#436)

* Migrate from failure to thiserror and anyhow

The failure crate invents its own traits that don't use
std::error::Error (because failure predates certain features added to
Error); this prevents using ? on an error from failure in a function
using Error. The thiserror and anyhow crates integrate with the standard
Error trait instead.

This change does not attempt to semantically change or refactor the
approach to error-handling in any portion of the code, to ensure that
the change remains straightforward to review. Modules using specific
differentiated error types move from failure_derive and derive(Fail) to
thiserror and derive(Error). Modules boxing all errors opaquely move
from failure::Error to anyhow. Modules using String as an error type
continue to do so. Code using unwrap or expect continues to do so.

Drop Display implementations when thiserror can easily derive an
identical instance.

Drop manual traversal of iter_causes; anyhow's Debug instance prints the
chain of causes by default.

Use anyhow's type alias anyhow::Result<T> in place of
std::result::Result<T, anyhow::Error> whenever possible.

* wasm2obj: Simplify error handling using existing messages

handle_module in wasm2obj manually maps
cranelift_codegen::isa::LookupError values to strings, but LookupError
values already have strings that say almost exactly the same thing.
Rely on the strings from cranelift.

* wasmtime: Rely on question-mark-in-main

The main() wrapper around rmain() completely matches the behavior of
question-mark-in-main (print error to stderr and return 1), so switch to
question-mark-in-main.

* Update to walrus 0.13 and wasm-webidl-bindings 0.6

Both crates switched from failure to anyhow; updating lets us avoid a
translation from failure to anyhow within wasmtime-interface-types.
This commit is contained in:
Josh Triplett
2019-11-04 20:43:25 -08:00
committed by Dan Gohman
parent 0108622c7d
commit 56ce6e9c9f
53 changed files with 238 additions and 326 deletions

View File

@@ -203,12 +203,7 @@ fn handle_module(
let isa_builder = match *target {
Some(ref target) => {
let target = Triple::from_str(&target).map_err(|_| "could not parse --target")?;
isa::lookup(target).map_err(|err| match err {
isa::LookupError::SupportDisabled => {
"support for architecture disabled at compile time"
}
isa::LookupError::Unsupported => "unsupported architecture",
})?
isa::lookup(target).map_err(|err| format!("{:?}", err))?
}
None => cranelift_native::builder().unwrap_or_else(|_| {
panic!("host machine is not a supported target");

View File

@@ -30,10 +30,10 @@
)
)]
use anyhow::{bail, Context as _, Result};
use cranelift_codegen::settings;
use cranelift_codegen::settings::Configurable;
use docopt::Docopt;
use failure::{bail, Error, ResultExt};
use pretty_env_logger;
use serde::Deserialize;
use std::collections::HashMap;
@@ -185,19 +185,7 @@ fn compute_environ(flag_env: &[String]) -> Vec<(String, String)> {
result
}
fn main() {
let err = match rmain() {
Ok(()) => return,
Err(e) => e,
};
eprintln!("error: {}", err);
for cause in err.iter_causes() {
eprintln!(" caused by: {}", cause);
}
exit(1);
}
fn rmain() -> Result<(), Error> {
fn main() -> Result<()> {
let version = env!("CARGO_PKG_VERSION");
let args: Args = Docopt::new(USAGE)
.and_then(|d| {
@@ -324,13 +312,13 @@ fn rmain() -> Result<(), Error> {
for filename in &args.flag_preload {
let path = Path::new(&filename);
instantiate_module(store.clone(), &module_registry, path)
.with_context(|_| format!("failed to process preload at `{}`", path.display()))?;
.with_context(|| format!("failed to process preload at `{}`", path.display()))?;
}
// Load the main wasm module.
let path = Path::new(&args.arg_file);
handle_module(store, &module_registry, &args, path)
.with_context(|_| format!("failed to process main module `{}`", path.display()))?;
.with_context(|| format!("failed to process main module `{}`", path.display()))?;
Ok(())
}
@@ -338,7 +326,7 @@ fn instantiate_module(
store: HostRef<Store>,
module_registry: &HashMap<String, (Instance, HashMap<String, usize>)>,
path: &Path,
) -> Result<(HostRef<Instance>, HostRef<Module>, Vec<u8>), Error> {
) -> Result<(HostRef<Instance>, HostRef<Module>, Vec<u8>)> {
// Read the wasm module binary either as `*.wat` or a raw binary
let data = wat::parse_file(path.to_path_buf())?;
@@ -378,7 +366,7 @@ fn handle_module(
module_registry: &HashMap<String, (Instance, HashMap<String, usize>)>,
args: &Args,
path: &Path,
) -> Result<(), Error> {
) -> Result<()> {
let (instance, _module, data) = instantiate_module(store.clone(), module_registry, path)?;
// If a function to invoke was given, invoke it.
@@ -396,7 +384,7 @@ fn invoke_export(
data: &ModuleData,
name: &str,
args: &Args,
) -> Result<(), Error> {
) -> Result<()> {
use wasm_webidl_bindings::ast;
use wasmtime_interface_types::Value;
@@ -445,7 +433,7 @@ fn invoke_export(
let mut context = store.borrow().engine().borrow().create_wasmtime_context();
let results = data
.invoke(&mut context, &mut handle, name, &values)
.with_context(|_| format!("failed to invoke `{}`", name))?;
.with_context(|| format!("failed to invoke `{}`", name))?;
if results.len() > 0 {
eprintln!(
"warning: using `--render` with a function that returns values \