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.
Metadata
Metadata
Assignees
Labels
No labels