-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
node-drivers: unwinding panics into napi errors, that can be converti…
…ble to quaint errors (#4106) * Wrap panics in a quaint::ErrorKind::RawConnectorError * chore: streamline nested blocks --------- Co-authored-by: jkomyno <skiabo97@gmail.com>
- Loading branch information
Showing
9 changed files
with
123 additions
and
39 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use futures::{Future, FutureExt}; | ||
use napi::Error as NapiError; | ||
use quaint::error::Error as QuaintError; | ||
use std::{any::Any, panic::AssertUnwindSafe}; | ||
|
||
/// transforms a napi error into a quaint error copying the status and reason | ||
/// properties over | ||
pub(crate) fn into_quaint_error(napi_err: NapiError) -> QuaintError { | ||
let status = napi_err.status.as_ref().to_owned(); | ||
let reason = napi_err.reason.clone(); | ||
|
||
QuaintError::raw_connector_error(status, reason) | ||
} | ||
|
||
/// catches a panic thrown during the executuin of an asynchronous closure and transforms it into | ||
/// the Error variant of a napi::Result. | ||
pub(crate) async fn async_unwinding_panic<F, R>(fut: F) -> napi::Result<R> | ||
where | ||
F: Future<Output = napi::Result<R>>, | ||
{ | ||
AssertUnwindSafe(fut) | ||
.catch_unwind() | ||
.await | ||
.unwrap_or_else(panic_to_napi_err) | ||
} | ||
|
||
/// catches a panic thrown during the execution of a closure and transforms it into a the Error | ||
/// variant of a napi::Result. | ||
pub(crate) fn unwinding_panic<F, R>(f: F) -> napi::Result<R> | ||
where | ||
F: Fn() -> napi::Result<R>, | ||
{ | ||
match std::panic::catch_unwind(std::panic::AssertUnwindSafe(f)) { | ||
Ok(result) => result, | ||
Err(panic_payload) => panic_to_napi_err(panic_payload), | ||
} | ||
} | ||
|
||
fn panic_to_napi_err<R>(panic_payload: Box<dyn Any + Send>) -> napi::Result<R> { | ||
panic_payload | ||
.downcast_ref::<&str>() | ||
.map(|s| -> String { (*s).to_owned() }) | ||
.or_else(|| panic_payload.downcast_ref::<String>().map(|s| s.to_owned())) | ||
.map(|message| Err(napi::Error::from_reason(format!("PANIC: {message}")))) | ||
.ok_or(napi::Error::from_reason("PANIC: unknown panic".to_string())) | ||
.unwrap() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters