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

Enum support for TryIntoJs, structured errors from Result #155

Merged
merged 24 commits into from
May 18, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
sync and async exceptions behave the same, added tests/examples
mkawalec committed May 18, 2021
commit cf0b1bba3f5af47d3c364e84760e7cd8df257596
12 changes: 12 additions & 0 deletions examples/json/src/lib.rs
Original file line number Diff line number Diff line change
@@ -90,4 +90,16 @@ fn with_fields() -> ErrorType {
#[node_bindgen]
fn with_unit() -> ErrorType {
ErrorType::UnitErrorType
}

#[node_bindgen]
fn failed_result_with_fields() -> Result<(), ErrorType> {
Err(ErrorType::WithFields {
val: 987
})
}

#[node_bindgen]
async fn async_result_failed_unit() -> Result<(), ErrorType> {
Err(ErrorType::UnitErrorType)
}
15 changes: 14 additions & 1 deletion examples/json/test.js
Original file line number Diff line number Diff line change
@@ -25,4 +25,17 @@ assert.deepStrictEqual(addon.withFields(), {
}, "named enum variant");
assert.deepStrictEqual(addon.withUnit(),
"UnitErrorType",
"unit enum variant")
"unit enum variant")

assert.throws(() => addon.failedResultWithFields(), {
"withFields": {
val: 987n
}
}, "sync exception");

assert.rejects(() => addon.asyncResultFailedUnit(),
(err) => {
assert.strictEqual(err, "UnitErrorType");
return true;
},
"async exception");
17 changes: 17 additions & 0 deletions nj-core/src/basic.rs
Original file line number Diff line number Diff line change
@@ -476,6 +476,23 @@ impl JsEnv {
pending
}

pub fn throw(&self, value: napi_value) {
debug!("throwing a native value");

// check if there is exception pending, if so log and not do anything
if self.is_exception_pending() {
error!(
"there is exception pending when trying to throw \
a native value, ignoring for now",
);
return;
}

unsafe {
crate::sys::napi_throw(self.inner(), value)
};
}

pub fn throw_type_error(&self, message: &str) {
debug!("throwing type error: {}", message);
// check if there is exception pending, if so log and not do anything
14 changes: 11 additions & 3 deletions nj-core/src/error.rs
Original file line number Diff line number Diff line change
@@ -24,9 +24,17 @@ pub enum NjError {
// errors are thrown
impl IntoJs for NjError {
fn into_js(self, js_env: &JsEnv) -> napi_value {
let msg = self.to_string();
js_env.throw_type_error(&msg);
ptr::null_mut()
match self {
NjError::Native(err) => {
js_env.throw(err);
ptr::null_mut()
},
_ => {
let msg = self.to_string();
js_env.throw_type_error(&msg);
ptr::null_mut()
}
}
}
}