fix(napi): better error message for #[napi(catch_unwind)] (#1383)

Before, the JS error would only have `Any { .. }`
This commit is contained in:
Simon Vandel Sillesen 2022-12-09 10:37:10 +00:00 committed by GitHub
parent e1864d68ff
commit 00b09bca6e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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)
}
}