napi-rs/examples/napi/src/error.rs
Markus 73a704a19e
feat(napi): keep stack traces in a deferred context (#1637)
* feat(napi): keep stack traces in deferred context

* chore: reformat code

Signed-off-by: Markus <28785953+MarkusJx@users.noreply.github.com>

* chore: use napi wrappers

Signed-off-by: Markus <28785953+MarkusJx@users.noreply.github.com>

* test(napi): add test for deferred trace

Signed-off-by: Markus <28785953+MarkusJx@users.noreply.github.com>

* chore: fix format

Signed-off-by: Markus <28785953+MarkusJx@users.noreply.github.com>

---------

Signed-off-by: Markus <28785953+MarkusJx@users.noreply.github.com>
2023-07-15 12:07:14 +08:00

40 lines
762 B
Rust

use napi::bindgen_prelude::*;
#[napi]
pub fn throw_error() -> Result<()> {
Err(Error::new(Status::InvalidArg, "Manual Error".to_owned()))
}
#[napi(catch_unwind)]
pub fn panic() {
panic!("Don't panic");
}
#[napi]
pub fn receive_string(s: String) -> String {
s
}
pub enum CustomError {
NapiError(Error<Status>),
Panic,
}
impl AsRef<str> for CustomError {
fn as_ref(&self) -> &str {
match self {
CustomError::Panic => "Panic",
CustomError::NapiError(e) => e.status.as_ref(),
}
}
}
#[napi]
pub fn custom_status_code() -> Result<(), CustomError> {
Err(Error::new(CustomError::Panic, "don't panic"))
}
#[napi]
pub async fn throw_async_error() -> Result<()> {
Err(Error::new(Status::InvalidArg, "Async Error".to_owned()))
}