fix(napi): better error message for #[napi(catch_unwind)]
(#1383)
Before, the JS error would only have `Any { .. }`
This commit is contained in:
parent
e1864d68ff
commit
00b09bca6e
1 changed files with 12 additions and 1 deletions
|
@ -140,7 +140,18 @@ impl TryToTokens for NapiFn {
|
||||||
quote! {
|
quote! {
|
||||||
{
|
{
|
||||||
std::panic::catch_unwind(|| { #function_call })
|
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)
|
.and_then(|r| r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue