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

How to throw an exception while requiring a module? #1199

Closed
ghost opened this issue Apr 4, 2018 · 2 comments
Closed

How to throw an exception while requiring a module? #1199

ghost opened this issue Apr 4, 2018 · 2 comments

Comments

@ghost
Copy link

ghost commented Apr 4, 2018

  • Node.js Version: v9.9.0
  • OS: windows 10

I'm trying to throw an error like this

void throwSomething(){
  Isolate *isolate = Isolate::GetCurrent();
  Local<String> msg = String::NewFromUtf8(isolate, "Something went wrong");
  isolate->ThrowException(Exception::TypeError(msg));
}

What I currently have

void test(const FunctionCallbackInfo<Value>& args){ throwSomething(); }
void init(Local<Object> exports, Local<Object> module){
  NODE_SET_METHOD(exports, "test", test);
}
NODE_MODULE(NODE_GYP_MODULE_NAME, init)

and now if I call require('test').test() it throws error properly. But I would like to throw the error while loading the module, not when the module is loaded.

What I would like to have

void init(Local<Object> exports, Local<Object> module){
  throwSomething();
}
NODE_MODULE(NODE_GYP_MODULE_NAME, init)

Now when I call require('test') the process exits immediately. I checked the exit code and it was 0, so the process didn't crash, but obviously the error has not been displayed. My assumption is that Isolate::GetCurrent() doesn't return the isolate I need while the module is loading and it yields in throwing an exception in the wrong isolate, but I am not sure if I am right.

I searched in the documentation and didn't find anything about throwing exception while loading a module. Maybe it would be useful (if the answer is simple enough) to include it in the documentation, imo it is sometimes very useful to perform some initializations while a module is loading and (in case these initialization fail) throw an exception indicating that the module has not been loaded properly.

Thanks.

@devsnek
Copy link
Member

devsnek commented Apr 7, 2018

this could possibly be related to nodejs/node#19437, although since you don't appear to be using napi i'm not sure. one thing i am sure of is that (at the current time) node only uses one isolate so triggering an exception on the wrong isolate isn't the issue.

/cc @nodejs/addon-api

@ghost
Copy link
Author

ghost commented Apr 8, 2018

@devsnek

Thanks. It seems that the problem is actually fixed in node v9.11.1. I just downloaded and tried, and the exception has been thrown properly. I didn't know this was actually a bug.

Closing, since I've found the answer. Thanks.

@ghost ghost closed this as completed Apr 8, 2018
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant