Migrate to the failure crate

Currently errors are reported via Rust panics but there's lots more errors being
added over time so this commit starts the movement towards the `failure` crate
to more idiomatically report errors as well as provide better error messages
over time.
This commit is contained in:
Alex Crichton
2018-04-25 11:42:22 -07:00
parent 2b9c48d5f9
commit 5f59d95130
10 changed files with 359 additions and 256 deletions

View File

@ -3,11 +3,15 @@ extern crate wasm_bindgen_cli_support;
extern crate serde_derive;
extern crate docopt;
extern crate wasm_bindgen_shared;
#[macro_use]
extern crate failure;
use std::path::PathBuf;
use std::process;
use docopt::Docopt;
use wasm_bindgen_cli_support::Bindgen;
use failure::Error;
const USAGE: &'static str = "
Generating JS bindings for a wasm file
@ -53,14 +57,25 @@ fn main() {
println!("wasm-bindgen {}", wasm_bindgen_shared::version());
return;
}
let err = match rmain(&args) {
Ok(()) => return,
Err(e) => e,
};
println!("error: {}", err);
for cause in err.causes().skip(1) {
println!("\tcaused by: {}", cause);
}
process::exit(1);
}
fn rmain(args: &Args) -> Result<(), Error> {
let input = match args.arg_input {
Some(s) => s,
None => panic!("input file expected"),
Some(ref s) => s,
None => bail!("input file expected"),
};
let mut b = Bindgen::new();
b.input_path(&input)
b.input_path(input)
.nodejs(args.flag_nodejs)
.browser(args.flag_browser)
.no_modules(args.flag_no_modules)
@ -73,8 +88,8 @@ fn main() {
let out_dir = match args.flag_out_dir {
Some(ref p) => p,
None => panic!("the `--out-dir` argument is now required"),
None => bail!("the `--out-dir` argument is now required"),
};
b.generate(out_dir).expect("failed to generate bindings");
b.generate(out_dir)
}

View File

@ -3,12 +3,16 @@ extern crate serde_derive;
extern crate docopt;
extern crate parity_wasm;
extern crate wasm_bindgen_cli_support;
#[macro_use]
extern crate failure;
use std::fs::File;
use std::io::{Write, Read};
use std::path::PathBuf;
use std::process;
use docopt::Docopt;
use failure::{Error, ResultExt};
const USAGE: &'static str = "
Converts a wasm file to an ES6 JS module
@ -42,38 +46,56 @@ fn main() {
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,
};
println!("error: {}", err);
for cause in err.causes().skip(1) {
println!("\tcaused by: {}", cause);
}
process::exit(1);
}
fn rmain(args: &Args) -> Result<(), Error> {
if !args.flag_base64 && !args.flag_fetch.is_some() {
panic!("unfortunately only works right now with base64 or fetch");
bail!("unfortunately only works right now with base64 or fetch");
}
let mut wasm = Vec::new();
File::open(&args.arg_input).expect("failed to open input")
.read_to_end(&mut wasm).expect("failed to read input");
File::open(&args.arg_input)
.and_then(|mut f| f.read_to_end(&mut wasm))
.with_context(|_| format!("failed to read `{}`", args.arg_input.display()))?;
let object = wasm_bindgen_cli_support::wasm2es6js::Config::new()
.base64(args.flag_base64)
.fetch(args.flag_fetch)
.generate(&wasm)
.expect("failed to parse wasm");
.fetch(args.flag_fetch.clone())
.generate(&wasm)?;
if args.flag_typescript {
if let Some(ref p) = args.flag_output {
let dst = p.with_extension("d.ts");
File::create(dst).expect("failed to create output")
.write_all(object.typescript().as_bytes()).expect("failed to write output");
let ts = object.typescript();
File::create(&dst)
.and_then(|mut f| f.write_all(ts.as_bytes()))
.with_context(|_| {
format!("failed to write `{}`", dst.display())
})?;
}
}
let js = object.js();
let js = object.js()?;
match args.flag_output {
Some(ref p) => {
File::create(p).expect("failed to create output")
.write_all(js.as_bytes()).expect("failed to write output");
File::create(p)
.and_then(|mut f| f.write_all(js.as_bytes()))
.with_context(|_| format!("failed to write `{}`", p.display()))?;
}
None => {
println!("{}", js);
}
}
Ok(())
}