Skip to content

Commit

Permalink
UnwrapThrowExt for Result now prints the error (#4035)
Browse files Browse the repository at this point in the history
  • Loading branch information
sokorototo committed Aug 1, 2024
1 parent 5b28b66 commit 5490ed2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@
* Update the Indexed DB API.
[#4027](https://github.com/rustwasm/wasm-bindgen/pull/4027)

* `UnwrapThrowExt for Result` now makes use of the required `Debug` bound to display the error as well.
[#4035](https://github.com/rustwasm/wasm-bindgen/pull/4035)

### Fixed

* Copy port from headless test server when using `WASM_BINDGEN_TEST_ADDRESS`.
Expand Down
26 changes: 26 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,32 @@ impl<T, E> UnwrapThrowExt<T> for Result<T, E>
where
E: core::fmt::Debug,
{
#[cfg_attr(debug_assertions, track_caller)]
fn unwrap_throw(self) -> T {
if cfg!(all(
target_arch = "wasm32",
not(any(target_os = "emscripten", target_os = "wasi"))
)) {
match self {
Ok(val) => val,
Err(err) => {
let loc = core::panic::Location::caller();
let msg = alloc::format!(
"`unwrap_throw` failed ({}:{}:{}): {:?}",
loc.file(),
loc.line(),
loc.column(),
err
);

throw_str(&msg)
}
}
} else {
self.unwrap()
}
}

#[cfg_attr(debug_assertions, track_caller)]
fn expect_throw(self, message: &str) -> T {
if cfg!(all(
Expand Down

0 comments on commit 5490ed2

Please sign in to comment.