-
Notifications
You must be signed in to change notification settings - Fork 469
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
std::exception not caught #1225
Comments
Do you have a simple recreate that we can turn into a test ? |
Hi @froody, you're right only the exception of type #include <napi.h>
Napi::Value MethodCppExcpetion(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
throw std::invalid_argument("Cpp exception.");
return env.Undefined();
}
Napi::Value MethodNapiExcpetion(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
throw Napi::Error::New(env, "Napi exception");
return env.Undefined();
}
Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports["MethodCppExcpetion"] = Napi::Function::New(env, MethodCppExcpetion);
exports["MethodNapiExcpetion"] = Napi::Function::New(env, MethodNapiExcpetion);
return exports;
}
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init) If you call 'use strict'
const addon = require('bindings')('addon')
addon.MethodCppExcpetion() you will get something like this:
If you call 'use strict'
const addon = require('bindings')('addon')
console.log(addon.MethodNapiExcpetion()) you will get something like this:
We had some discusssion about this in the past in this issue #601. The proble is the we don't know what you want to do when a C++ exception will thrown and for now we leaved this respnsibility to the developer bucause we were unsure that trasform a Cpp exception could be the best option. At least the documentation should be changed, do you want to make a PR? |
Discussed in the Node-API team meeting today and @NickNaso is going to update the docs to make it clearer. |
I'm closing we updated the documentation through this PR #1241. |
The documentation states "On return from a native method, node-addon-api will automatically convert a pending C++ exception to a JavaScript exception." but in
InstanceMethodCallbackWrapper
when I call a C++ API that throwsstd::exception
,std::terminate
is called because no exception handler is installed. If I wrap my call in something likeThen the exceptions are passed up to javascript, but based on my reading of the documentation I shouldn't need to do this.
The text was updated successfully, but these errors were encountered: