From 030faf231b11087fa4faff744894b05404bfb6b2 Mon Sep 17 00:00:00 2001 From: sokorototo Date: Thu, 1 Aug 2024 17:04:43 +0300 Subject: [PATCH 1/2] improved error reporting for UnwrapThrowExt for Result --- src/lib.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index ec8a847933a..34b533bdfd6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1381,6 +1381,32 @@ impl UnwrapThrowExt for Result 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( From ec9a1f3ef24418be3baedc54e2f66f57fc5dfeff Mon Sep 17 00:00:00 2001 From: daxpedda Date: Thu, 1 Aug 2024 20:36:04 +0200 Subject: [PATCH 2/2] Add changelog entry --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 381699686aa..5295fd4f95c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`.