From 5ff477c952b3ffc72d78734e545592c70709fcf9 Mon Sep 17 00:00:00 2001 From: Simon Vandel Sillesen Date: Fri, 9 Dec 2022 09:49:23 +0000 Subject: [PATCH] Better error message for `#[napi(catch_unwind)]` Before, the JS error would only have `Any { .. }` --- crates/backend/src/codegen/fn.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/crates/backend/src/codegen/fn.rs b/crates/backend/src/codegen/fn.rs index 7ccff0ddcf..c724b8d94f 100644 --- a/crates/backend/src/codegen/fn.rs +++ b/crates/backend/src/codegen/fn.rs @@ -140,7 +140,18 @@ impl TryToTokens for NapiFn { quote! { { std::panic::catch_unwind(|| { #function_call }) - .map_err(|e| napi::Error::new(napi::Status::GenericFailure, format!("{:?}", e))) + .map_err(|e| { + let message = { + if let Some(string) = e.downcast_ref::() { + string.clone() + } else if let Some(string) = e.downcast_ref::<&str>() { + string.to_string() + } else { + format!("panic from Rust code: {:?}", e) + } + }; + napi::Error::new(napi::Status::GenericFailure, message) + }) .and_then(|r| r) } }