Skip to content

Commit

Permalink
fix(napi): better error message for #[napi(catch_unwind)] (#1383)
Browse files Browse the repository at this point in the history
Before, the JS error would only have `Any { .. }`
  • Loading branch information
simonvandel committed Dec 9, 2022
1 parent e1864d6 commit 00b09bc
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion crates/backend/src/codegen/fn.rs
Expand Up @@ -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>() {
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)
}
}
Expand Down

0 comments on commit 00b09bc

Please sign in to comment.