-
Notifications
You must be signed in to change notification settings - Fork 462
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
Callback exception catch and rethrow #271
Comments
Hi @bsrdjan, |
Hi @NickNaso, thank you very much for examples. I checked also issues N-API #235 and nodejs #15371 and tried following: // AsyncWorker...
void OnOK()
{
client->alive = (client->errorInfo.code == RFC_OK);
try
{
if (!client->alive)
{
Callback().Call({wrapError(&client->errorInfo)});
}
else
{
Callback().Call({});
}
}
catch (const Error &e)
{
//Napi::EscapableHandleScope scope(Callback().Env());
printf("rethrow...\n");
e.ThrowAsJavaScriptException();
}
} // test javascript
function test(err) {
console.log('invoked');
const m = 1;
const n = m + z; // <- exception
}
try {
test();
} catch (ex) {
console.log('Caught js');
assert(ex.name === 'ReferenceError');
assert(ex.message === 'z is not defined');
assert(ex.stack.length > 0);
}
try {
client.connect(test);
} catch (ex) {
console.log('Caught n-api');
} The test output is not as expected, the exception is rethrown from AsyncWorker but not get cought in JS. Instead of exception, I would expect seeing // test output
invoked
Caught js
invoked
rethrow...
/home/www-admin/src/node-rfc/exp/exception.js:15
const n = m + z;
^
ReferenceError: z is not defined Any ideas what could be wrong here? Platform is Linux, with "cflags!": ["-fno-exceptions"],
"cflags_cc!": ["-fno-exceptions"],
"defines": [
"SAPwithUNICODE",
"SAPwithTHREADS",
"NDEBUG",
"NAPI_CPP_EXCEPTIONS"
], |
after some more analysis, found the behaviour is standard for JavaScript, no issues with addon. |
How to catch and rethrow callback exceptions, replacing following Nan block with Napi?
The text was updated successfully, but these errors were encountered: