infinyon / infinyon/node-bindgen

Producing Errors for JS in Class Constructor

Open
#141 7 comments 0 reactions 1 assignee View on GitHub

Nobody has claimed this yet.

triage
Dominant language
Rust
Stars
587
Forks
43
PR merge metrics
No merged PRs in 30d

Description

trivially
```rust
struct MyClass {
a_string: String
}

#[node_bindgen]
impl MyClass {
#[node_bindgen(constructor)]
fn new() -> Self {
Self {
a_string: function_that_returns_result().unwrap()
}
}
}
```
This example causes the JS application to crash completely if the function returns an error, which would be great if that's what I wanted, but instead, I would like to be able to have the constructor error out, producing an error to be caught by JS instead of returning Self.
Now I'm newish to rust so maybe I missed something but the only way I understand to get an error up the call stack is to keep returning Results, however `#[node_bindgen(constructor)]` only accepts a return of "`Self`."
Understandably it could be possible to make `a_string` type `Result`, however, I would have to check it in every function and that simply causes problems for certain more complex objects that I intend to use.

So I am here to ask, how do I throw errors for catching in JS from my struct "constructor"?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.