Skip to content

Commit c679f6f

Browse files
authored
feat: allow catching all exceptions (#1593)
* feat: allow catching all exceptions * Address review comments
1 parent 0776de2 commit c679f6f

10 files changed

+237
-77
lines changed

doc/cmake-js.md

+19
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,25 @@ The following line in the `CMakeLists.txt` file will enable Node-API experimenta
4545
add_definitions(-DNAPI_EXPERIMENTAL)
4646
```
4747

48+
### Exception Handling
49+
50+
To enable C++ exception handling (for more info see: [Setup](setup.md)), define
51+
the corresponding preprocessor directives depending on which exception handling
52+
behavior is desired.
53+
54+
To enable C++ exception handling with `Napi::Error` objects only:
55+
56+
```
57+
add_definitions(-DNAPI_EXPERIMENTAL)
58+
```
59+
60+
To enable C++ exception handling for all exceptions thrown:
61+
62+
```
63+
add_definitions(-DNODE_ADDON_API_CPP_EXCEPTIONS)
64+
add_definitions(-DNODE_ADDON_API_CPP_EXCEPTIONS_ALL)
65+
```
66+
4867
### node-addon-api
4968

5069
If your Node-API native add-on uses the optional [**node-addon-api**](https://github.com/nodejs/node-addon-api#node-addon-api-module) C++ wrapper, the `CMakeLists.txt` file requires additional configuration information as described on the [CMake.js README file](https://github.com/cmake-js/cmake-js#node-api-and-node-addon-api).

doc/error_handling.md

+12
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ method.
4848
If a C++ exception of type `Napi::Error` escapes from a Node-API C++ callback, then
4949
the Node-API wrapper automatically converts and throws it as a JavaScript exception.
5050

51+
If other types of C++ exceptions are thrown, node-addon-api will either abort
52+
the process or wrap the exception in an `Napi::Error` in order to throw it as a
53+
JavaScript exception. This behavior is determined by which node-gyp dependency
54+
used:
55+
56+
- When using the `node_addon_api_except` dependency, only `Napi::Error` objects
57+
will be handled.
58+
- When using the `node_addon_api_except_all` dependency, all exceptions will be
59+
handled. For exceptions derived from `std::exception`, an `Napi::Error` will be
60+
created with the message of the exception's `what()` member function. For all
61+
other exceptions, an `Napi::Error` will be created with a generic error message.
62+
5163
On return from a native method, node-addon-api will automatically convert a pending
5264
`Napi::Error` C++ exception to a JavaScript exception.
5365

doc/setup.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,26 @@ To use **Node-API** in a native module:
3838
],
3939
```
4040

41-
To enable that capability, add an alternative dependency in `binding.gyp`:
41+
To enable that capability, add an alternative dependency in `binding.gyp`
42+
depending on if you want to integrate C++ exception handling for exceptions
43+
derived from `Napi::Error` or all C++ exceptions. To catch only
44+
`Napi::Error` exceptions, use:
4245

4346
```gyp
4447
'dependencies': [
4548
"<!(node -p \"require('node-addon-api').targets\"):node_addon_api_except",
4649
],
4750
```
4851

52+
Or, to allow catching all native C++ exceptions, use the
53+
`node_addon_api_except_all` dependency:
54+
55+
```gyp
56+
'dependencies': [
57+
"<!(node -p \"require('node-addon-api').targets\"):node_addon_api_except_all",
58+
],
59+
```
60+
4961
If you decide to use node-addon-api without C++ exceptions enabled, please
5062
consider enabling node-addon-api safe API type guards to ensure the proper
5163
exception handling pattern:

0 commit comments

Comments
 (0)