Repository navigation
Error messages and MakeCallback #235
Copy link
Copy link
Closed
Description
Lets say we have the below addon where RunCallbackWithCall uses Call to call a callback and RunCallbackWithMakeCallback uses MakeCallback to call a callback.
#include <napi.h>
void RunCallbackWithCall(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::Function cb = info[0].As<Napi::Function>();
cb.Call({ Napi::String::New(env, "Hello, World!") });
}
void RunCallbackWithMakeCallback(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
Napi::Function cb = info[0].As<Napi::Function>();
cb.MakeCallback(env.Global(), { Napi::String::New(env, "Hello, World!") });
}
Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports["runCallbackWithCall"] =
Napi::Function::New(env, RunCallbackWithCall);
exports["runCallbackWithMakeCallback"] =
Napi::Function::New(env, RunCallbackWithMakeCallback);
return exports;
}
NODE_API_MODULE(hello, Init)If the callback passed to runCallbackWithCall throws an Error like this:
const addon = require('bindings')('addon.node');
addon.runCallbackWithCall((str) => {
console.log(str);
throw new Error('Error in callback');
});Then the following is displayed:
Hello, World!
/home/pi/node-addon-api-experiments2/test/run-callback-with-call.js:5
addon.runCallbackWithCall((str) => {
^
Error: Error in callback
at addon.runCallbackWithCall (/home/pi/node-addon-api-experiments2/test/run-callback-with-call.js:7:9)
at Object.<anonymous> (/home/pi/node-addon-api-experiments2/test/run-callback-with-call.js:5:7)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3
Everything looks ok here. As is to be expected "Error: Error in callback" is displayed.
However, If the callback passed to runCallbackWithMakeCallback throws an Error like this:
const addon = require('bindings')('addon.node');
addon.runCallbackWithMakeCallback((str) => {
console.log(str);
throw new Error('Error in callback');
});Then the following is displayed:
Hello, World!
/home/pi/node-addon-api-experiments2/test/run-callback-with-makecallback.js:5
addon.runCallbackWithMakeCallback((str) => {
^
Error: Unknown failure
at Object.<anonymous> (/home/pi/node-addon-api-experiments2/test/run-callback-with-makecallback.js:5:7)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3
Things don't look correct here. I would expect "Error: Error in callback" to be displayed but "Error: Unknown failure" is displayed.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Activity
mhdawson commentedon Mar 14, 2018
Can youu attach a zip with the all of the files so that I can easily run/recreate?
mhdawson commentedon Mar 14, 2018
Error: Unknown failureis one of the messages used within the C n-api so either we are failing to preserve the original error message in the n-api call, or a later call to n-api is made while the exception is pending.Travelling today but will try to investigate further later this week. If you can give me a zip I can just unzip and run to recreate that will help get me started faster.
NickNaso commentedon Mar 14, 2018
Hi everyone,
I tried to recreate the same example proposed by @fivdi and I got the same behaviour. I post my project in the zip file hope to be helpful.
example-make-callback.zip
I don't know if this is correlated with this issue nodejs/node#15371
mhdawson commentedon Mar 14, 2018
I investigated on the plane today:
@NickNaso thanks, I'll use your files to validate the fix.
I believe it is unrelated to nodejs/node#15371
mhdawson commentedon Mar 14, 2018
Seems to be a bit more subtle than I thought, investigating further.
n-api: add missing exception checking
mhdawson commentedon Mar 15, 2018
Ok so there was a combination of factors here:
See nodejs/node#19362 for the PR to node core to fix.
mhdawson commentedon Mar 15, 2018
I'll have to take another look at nodejs/node#15371 as well since I'm not sure its unrelated after all.
fivdi commentedon Mar 16, 2018
Nice 😄
n-api: add missing exception checking
n-api: add missing exception checking
n-api: add missing exception checking
4 remaining items
n-api: add missing exception checking
n-api: add missing exception checking
n-api: add missing exception checking