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

Callback exception catch and rethrow #271

Closed
bsrdjan opened this issue May 15, 2018 · 3 comments
Closed

Callback exception catch and rethrow #271

bsrdjan opened this issue May 15, 2018 · 3 comments

Comments

@bsrdjan
Copy link

bsrdjan commented May 15, 2018

How to catch and rethrow callback exceptions, replacing following Nan block with Napi?

Nan::TryCatch try_catch;

Local<Function> callback = Nan::New<Function>(baton->callback);
Nan::Call(callback, Nan::New<Object>(), 2, argv);

if (try_catch.HasCaught())
{
    Nan::FatalException(try_catch);
}
@NickNaso
Copy link
Member

Hi @bsrdjan,
you could take a look at this example from the test case.
https://github.com/nodejs/node-addon-api/blob/master/test/error.cc#L34
We are working on error handling documentation. I hope that it will be available very soon.

@bsrdjan
Copy link
Author

bsrdjan commented May 17, 2018

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 Caught n-api in log:

// 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 binding.gyp config

            "cflags!": ["-fno-exceptions"],
            "cflags_cc!": ["-fno-exceptions"],

            "defines": [
                "SAPwithUNICODE",
                "SAPwithTHREADS",
                "NDEBUG",
                "NAPI_CPP_EXCEPTIONS"
            ],

@bsrdjan
Copy link
Author

bsrdjan commented May 18, 2018

after some more analysis, found the behaviour is standard for JavaScript, no issues with addon.

@bsrdjan bsrdjan closed this as completed May 18, 2018
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

2 participants