Reputation: 628
Lets say I have a JS function which is simply throwing an error:
function() { throw "Danger, Will Robinson!"; }
This function passed in as an argument to a node.js addon and is used to construct a Nan::Callback (which should take care of making this handle persistent):
// get handle to JS function and its Tcl name
Handle<Function> fun = Handle<Function>::Cast( info[0] );
Nan::Callback *pf = new Nan::Callback(fun);
I'm having problems intercepting this JS exception from C++ when the Nan::Callback
is called with Call()
from C++:
Nan::TryCatch tc;
Local<Value> retv = pf->Call( Nan::GetCurrentContext()->Global(), objc-1, &args );
if ( tc.HasCaught() ) {
printf("EXCEPTION:\n");
...
In fact the script simply exits at the JS error and I never get back to inspect tc
and the calls return value (retv
) for any pending exceptions. What am I doing wrong?
Upvotes: 2
Views: 1149
Reputation: 628
Found it, this is most probably a known Nan bug. Quoting hankbao's comment on 27 Jul:
I've just encountered a related issue. We're currently using electron to pack our web app. There's a native node addon providing some functionality for the electron app. Our web app will call the addon and pass a js callback to it. The native addon store the callback in a NanCallback. It setups a TryCatch before calling the callback. However the TryCatch always fails to catch the exception thrown by the js callback. It turns out that if I call the callback with Function::Call instead of with NanCallback::Call(), exceptions can be caught
Based on this hint, I got the issue solved: I replaced the function handle from Nan::Callback
with v8::Persistent<Function>
, and finally got TryCatch
to work as expected. The only catch: not using Nan means the code's prone to break as v8 is not exactly a stable API over time :)
Upvotes: 3