Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error message for #[napi(catch_unwind)] #1383

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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