node-addon-examples icon indicating copy to clipboard operation
node-addon-examples copied to clipboard

Methods not returning after throwing Javascript exception

Open DanyUP opened this issue 7 years ago • 2 comments

In the "6_object_wrap_example/node-addon-api", I tried to test what would happen when calling the constructor without arguments and I noticed that the actual exception thrown ("A number was expected") is different from the one in the code ("Number expected").

According to node-addon-api docs, when C++ exceptions are disabled you need to return immediately after calling ThrowAsJavaScriptException. So I would expect something like:

if (length <= 0 || !info[0].IsNumber()) {
    Napi::TypeError::New(env, "Number expected").ThrowAsJavaScriptException();
    return;
}

I think that the exception thrown is not the one from line 28 of myobject.cc, but another one thrown automatically when trying to convert the parameter to a number.

I tried to move one step further (following this tutorial) and unwrapping a Javascript object, and I get a segmentation fault when using the constructor without arguments because the code is not stopping after reaching the line with ThrowAsJavaScriptException.

DanyUP avatar Oct 14 '18 09:10 DanyUP

Do you have the code for the simple case where you get a segmentation fault?

mhdawson avatar Jan 30 '19 18:01 mhdawson

An issue in the node-addon-api repo might also be better unless you think the problem is in the example as opposed to the node-addon-api implementation.

mhdawson avatar Jan 30 '19 18:01 mhdawson