This does two big things: - We add custom error messages and conversions from other error types into our own error type. This means we can create or modify more fine grained errors rather than using whatever `failure::Error` gives us. The first one we have is a `Cli` error, whereby calls to things like wasm-bindgen or npm failed. - We also make it so that `PBAR.error()` is called only on exit. We grab a reference to whatever error gets passed up the chain, have it generate a message to be printed as an error to PBAR, close up PBAR like normal, and then print our actual error message to stderr! The main benefits of this is that as long as we return an error, PBAR will print and close itself up and we don't need to call it every time or forget that we would need to call it, and we put error details specifically into our error! Closes #12master
parent
26c8b8dfcf
commit
b8763a3285
@ -0,0 +1,57 @@ |
||||
//! Code related to error handling for wasm-pack
|
||||
use serde_json; |
||||
use std::borrow::Cow; |
||||
use std::io; |
||||
use toml; |
||||
|
||||
#[derive(Debug, Fail)] |
||||
pub enum Error { |
||||
/// Maps any underlying I/O errors that are thrown to this variant
|
||||
#[fail(display = "{}", _0)] |
||||
Io(#[cause] io::Error), |
||||
#[fail(display = "{}", _0)] |
||||
SerdeJson(#[cause] serde_json::Error), |
||||
#[fail(display = "{}", _0)] |
||||
SerdeToml(#[cause] toml::de::Error), |
||||
#[fail(display = "{}. stderr:\n\n{}", message, stderr)] |
||||
Cli { message: String, stderr: String }, |
||||
} |
||||
|
||||
impl Error { |
||||
pub fn cli(message: &str, stderr: Cow<str>) -> Result<(), Self> { |
||||
Err(Error::Cli { |
||||
message: message.to_string(), |
||||
stderr: stderr.to_string(), |
||||
}) |
||||
} |
||||
|
||||
pub fn error_type(&self) -> String { |
||||
match self { |
||||
Error::Io(_) => "There was an I/O error. Details:\n\n", |
||||
Error::SerdeJson(_) => "There was an JSON error. Details:\n\n", |
||||
Error::SerdeToml(_) => "There was an TOML error. Details:\n\n", |
||||
Error::Cli { |
||||
message: _, |
||||
stderr: _, |
||||
} => "There was an error while calling another CLI tool. Details:\n\n", |
||||
}.to_string() |
||||
} |
||||
} |
||||
|
||||
impl From<io::Error> for Error { |
||||
fn from(e: io::Error) -> Self { |
||||
Error::Io(e) |
||||
} |
||||
} |
||||
|
||||
impl From<serde_json::Error> for Error { |
||||
fn from(e: serde_json::Error) -> Self { |
||||
Error::SerdeJson(e) |
||||
} |
||||
} |
||||
|
||||
impl From<toml::de::Error> for Error { |
||||
fn from(e: toml::de::Error) -> Self { |
||||
Error::SerdeToml(e) |
||||
} |
||||
} |
Loading…
Reference in new issue