Skip to content

Commit

Permalink
Better display
Browse files Browse the repository at this point in the history
  • Loading branch information
fl0rek committed Jul 3, 2024
1 parent ce14712 commit 637ae67
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions node-wasm/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! Error type and utilities.
use std::fmt::Display;
use std::fmt::{Display, self};

use serde::{Deserialize, Serialize};
use wasm_bindgen::convert::IntoWasmAbi;
use wasm_bindgen::describe::WasmDescribe;
use wasm_bindgen::JsValue;
use wasm_bindgen::{JsCast, JsValue};

/// Alias for a `Result` with the error type [`Error`].
pub type Result<T, E = Error> = std::result::Result<T, E>;
Expand Down Expand Up @@ -50,6 +50,29 @@ impl Error {
}
}

impl Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::fmt::Result {
let Some(error) = self.0.dyn_ref::<js_sys::Error>() else {
return write!(f, "{:?}", self.0.as_string());
};

write!(f, "{} ({})", error.name(), error.message())?;

let mut cause = error.cause();
loop {
if let Some(error) = cause.dyn_ref::<js_sys::Error>() {
write!(f, "\n{} ({})", error.name(), error.message())?;
cause = error.cause();
} else {
write!(f, "\n{:?}", cause.as_string())?;
break;
}
}

Ok(())
}
}

// Similar to https://github.com/rustwasm/wasm-bindgen/blob/9b37613bbab0cd71f55dcc782d59dd00c1d4d200/src/describe.rs#L218-L222
//
// This is needed to impl IntoWasmAbi.
Expand Down

0 comments on commit 637ae67

Please sign in to comment.