Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improved error reporting for UnwrapThrowExt for Result #4035

Merged
merged 2 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"))
)) {
Comment on lines +1386 to +1389
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be gated behind cfg(debug_assertions), the same as the default unwrap_throw implementation; see #2995 (review).

(The cfg(feature = "std") might not be needed anymore after #4005.)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!
Addressed in #4042.

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