Skip to content

Commit cf186ac

Browse files
authored
Print location when using expect_throw() (#4049)
1 parent 4596d4a commit cf186ac

File tree

2 files changed

+75
-22
lines changed

2 files changed

+75
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115

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

119120
* MSRV of CLI tools bumped to v1.76. This does not affect libraries like `wasm-bindgen`, `js-sys` and `web-sys`!
120121
[#4037](https://github.com/rustwasm/wasm-bindgen/pull/4037)

src/lib.rs

Lines changed: 74 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,14 +1351,15 @@ pub trait UnwrapThrowExt<T>: Sized {
13511351
)) {
13521352
let loc = core::panic::Location::caller();
13531353
let msg = alloc::format!(
1354-
"`unwrap_throw` failed ({}:{}:{})",
1354+
"called `{}::unwrap_throw()` ({}:{}:{})",
1355+
core::any::type_name::<Self>(),
13551356
loc.file(),
13561357
loc.line(),
13571358
loc.column()
13581359
);
13591360
self.expect_throw(&msg)
13601361
} else {
1361-
self.expect_throw("`unwrap_throw` failed")
1362+
self.expect_throw("called `unwrap_throw()`")
13621363
}
13631364
}
13641365

@@ -1376,11 +1377,43 @@ pub trait UnwrapThrowExt<T>: Sized {
13761377
}
13771378

13781379
impl<T> UnwrapThrowExt<T> for Option<T> {
1380+
fn unwrap_throw(self) -> T {
1381+
const MSG: &str = "called `Option::unwrap_throw()` on a `None` value";
1382+
1383+
if cfg!(all(target_arch = "wasm32", target_os = "unknown")) {
1384+
if let Some(val) = self {
1385+
val
1386+
} else if cfg!(debug_assertions) {
1387+
let loc = core::panic::Location::caller();
1388+
let msg =
1389+
alloc::format!("{} ({}:{}:{})", MSG, loc.file(), loc.line(), loc.column(),);
1390+
1391+
throw_str(&msg)
1392+
} else {
1393+
throw_str(MSG)
1394+
}
1395+
} else {
1396+
self.expect(MSG)
1397+
}
1398+
}
1399+
13791400
fn expect_throw(self, message: &str) -> T {
13801401
if cfg!(all(target_arch = "wasm32", target_os = "unknown")) {
1381-
match self {
1382-
Some(val) => val,
1383-
None => throw_str(message),
1402+
if let Some(val) = self {
1403+
val
1404+
} else if cfg!(debug_assertions) {
1405+
let loc = core::panic::Location::caller();
1406+
let msg = alloc::format!(
1407+
"{} ({}:{}:{})",
1408+
message,
1409+
loc.file(),
1410+
loc.line(),
1411+
loc.column(),
1412+
);
1413+
1414+
throw_str(&msg)
1415+
} else {
1416+
throw_str(message)
13841417
}
13851418
} else {
13861419
self.expect(message)
@@ -1393,36 +1426,55 @@ where
13931426
E: core::fmt::Debug,
13941427
{
13951428
fn unwrap_throw(self) -> T {
1396-
if cfg!(all(
1397-
debug_assertions,
1398-
target_arch = "wasm32",
1399-
target_os = "unknown"
1400-
)) {
1429+
const MSG: &str = "called `Result::unwrap_throw()` on an `Err` value";
1430+
1431+
if cfg!(all(target_arch = "wasm32", target_os = "unknown")) {
14011432
match self {
14021433
Ok(val) => val,
14031434
Err(err) => {
1404-
let loc = core::panic::Location::caller();
1405-
let msg = alloc::format!(
1406-
"`unwrap_throw` failed ({}:{}:{}): {:?}",
1407-
loc.file(),
1408-
loc.line(),
1409-
loc.column(),
1410-
err
1411-
);
1412-
1413-
throw_str(&msg)
1435+
if cfg!(debug_assertions) {
1436+
let loc = core::panic::Location::caller();
1437+
let msg = alloc::format!(
1438+
"{} ({}:{}:{}): {:?}",
1439+
MSG,
1440+
loc.file(),
1441+
loc.line(),
1442+
loc.column(),
1443+
err
1444+
);
1445+
1446+
throw_str(&msg)
1447+
} else {
1448+
throw_str(MSG)
1449+
}
14141450
}
14151451
}
14161452
} else {
1417-
self.expect("`unwrap_throw` failed")
1453+
self.expect(MSG)
14181454
}
14191455
}
14201456

14211457
fn expect_throw(self, message: &str) -> T {
14221458
if cfg!(all(target_arch = "wasm32", target_os = "unknown")) {
14231459
match self {
14241460
Ok(val) => val,
1425-
Err(_) => throw_str(message),
1461+
Err(err) => {
1462+
if cfg!(debug_assertions) {
1463+
let loc = core::panic::Location::caller();
1464+
let msg = alloc::format!(
1465+
"{} ({}:{}:{}): {:?}",
1466+
message,
1467+
loc.file(),
1468+
loc.line(),
1469+
loc.column(),
1470+
err
1471+
);
1472+
1473+
throw_str(&msg)
1474+
} else {
1475+
throw_str(message)
1476+
}
1477+
}
14261478
}
14271479
} else {
14281480
self.expect(message)

0 commit comments

Comments
 (0)