-
-
Notifications
You must be signed in to change notification settings - Fork 239
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
Throwing custom error subclass object #1981
Comments
Isn't this a behaviour that is working? If you throw an error subclass, and it implements Would you want a way to parse the custom error type from within Rust? Do you have a code snippet of how you'd expect it to work? |
Hi @lbarthon, The error implementation uses a temporary Rust value which will be converted to a Js value at correct time. If a custom error subclass instance is passed from Js, the Rust error cannot retrieve all of its fields. There is no way to create an error of an Error subclass in Rust. Developers from the Js side use The See this https://github.com/jshttp/http-errors, for node developers, it makes sense to define an error subclass with custom fields. To support this, several things could be done:
|
I too want to throw instances of specific subclasses of (the JS global) One idea I had was to expose a function (I can simply use a constructor in my case) from Rust to be called by JS to inject the constructors for these custom errors, enabling Rust to create instances of them. But .. to be honest, I'm struggling with declaring the type of the function taking these Error constructors 🤔 Also, I noticed #1205 while searching the repo for other issues related to this. |
This example https://pyo3.rs/v0.20.3/exception demonstrates how to import and expose platform errors in Rust code. This is how pyo3 implemented for Python. |
I've made a bit of progress on a workaround for this as I noticed the napi-rs/crates/napi/src/error.rs Lines 83 to 111 in 368e210
Inject and fail in a single callMore specifically, it's possible to create an instance of a custom error and construct a use napi::{bindgen_prelude::*, JsUndefined};
#[macro_use]
extern crate napi_derive;
#[napi]
pub fn inject_and_fail(error_constructor: JsFunction) -> napi::Result<JsUndefined> {
let args: Vec<napi::JsUnknown> = vec![];
let error = error_constructor.new_instance(&args).unwrap();
Err(napi::Error::from(error.into_unknown()))
} Inject and fail across callsBut ... for my use-case I want to inject the custom error from JS and store it for later use by another function. use napi::{bindgen_prelude::*, JsUndefined};
#[macro_use]
extern crate napi_derive;
thread_local! {
static ERROR_CONSTRUCTOR: RefCell<JsFunction> = panic!("Error constructor accessed before injection");
}
#[napi]
pub fn inject(error_constructor: napi::JsFunction) {
ERROR_CONSTRUCTOR.set(error_constructor);
}
#[napi]
pub fn fail() -> napi::Result<JsUndefined> {
let args: Vec<napi::JsUnknown> = vec![];
let error = ERROR_CONSTRUCTOR
.with(|error_constructor| error_constructor.borrow().new_instance(&args).unwrap());
Err(napi::Error::from(error.into_unknown()))
} Perhaps someone more knowledgeable can hint to the underlying issue? |
Hi @kraenhansen, To retrieve the data in the error in Rust, an There are several scenarios that a cross error is used:
It's hard to implement unless the current error strategy is fully updated. The current API that returns an Err variant of Hi @Brooooooklyn, could you give us some ideas on doing this? |
Hi @Brooooooklyn,
Seems like the
maybe_raw
field of theError
struct, represents the error object in Js. How can I define and throw an Error of a custom Error subclass? Sometimes users want to throw something like https://github.com/jshttp/http-errors,new HTTPError("MyBadRequest", 400)
.I understand that you are busy, can you give me some instructions on how to implement this? I can implement and fire a PR.
The text was updated successfully, but these errors were encountered: