From 00b09bca6ef520663d9ce9a3348af7c367b5ceee Mon Sep 17 00:00:00 2001 From: Simon Vandel Sillesen Date: Fri, 9 Dec 2022 10:37:10 +0000 Subject: [PATCH] fix(napi): better error message for `#[napi(catch_unwind)]` (#1383) 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 7ccff0dd..c724b8d9 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) } }