Skip to content

Commit

Permalink
feat: allow catching all exceptions (#1593)
Browse files Browse the repository at this point in the history
* feat: allow catching all exceptions

* Address review comments
  • Loading branch information
KevinEady authored Nov 15, 2024
1 parent 0776de2 commit c679f6f
Show file tree
Hide file tree
Showing 10 changed files with 237 additions and 77 deletions.
19 changes: 19 additions & 0 deletions doc/cmake-js.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ The following line in the `CMakeLists.txt` file will enable Node-API experimenta
add_definitions(-DNAPI_EXPERIMENTAL)
```

### Exception Handling

To enable C++ exception handling (for more info see: [Setup](setup.md)), define
the corresponding preprocessor directives depending on which exception handling
behavior is desired.

To enable C++ exception handling with `Napi::Error` objects only:

```
add_definitions(-DNAPI_EXPERIMENTAL)
```

To enable C++ exception handling for all exceptions thrown:

```
add_definitions(-DNODE_ADDON_API_CPP_EXCEPTIONS)
add_definitions(-DNODE_ADDON_API_CPP_EXCEPTIONS_ALL)
```

### node-addon-api

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).
Expand Down
12 changes: 12 additions & 0 deletions doc/error_handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ method.
If a C++ exception of type `Napi::Error` escapes from a Node-API C++ callback, then
the Node-API wrapper automatically converts and throws it as a JavaScript exception.

If other types of C++ exceptions are thrown, node-addon-api will either abort
the process or wrap the exception in an `Napi::Error` in order to throw it as a
JavaScript exception. This behavior is determined by which node-gyp dependency
used:

- When using the `node_addon_api_except` dependency, only `Napi::Error` objects
will be handled.
- When using the `node_addon_api_except_all` dependency, all exceptions will be
handled. For exceptions derived from `std::exception`, an `Napi::Error` will be
created with the message of the exception's `what()` member function. For all
other exceptions, an `Napi::Error` will be created with a generic error message.

On return from a native method, node-addon-api will automatically convert a pending
`Napi::Error` C++ exception to a JavaScript exception.

Expand Down
14 changes: 13 additions & 1 deletion doc/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,26 @@ To use **Node-API** in a native module:
],
```

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

```gyp
'dependencies': [
"<!(node -p \"require('node-addon-api').targets\"):node_addon_api_except",
],
```

Or, to allow catching all native C++ exceptions, use the
`node_addon_api_except_all` dependency:

```gyp
'dependencies': [
"<!(node -p \"require('node-addon-api').targets\"):node_addon_api_except_all",
],
```

If you decide to use node-addon-api without C++ exceptions enabled, please
consider enabling node-addon-api safe API type guards to ensure the proper
exception handling pattern:
Expand Down
Loading

0 comments on commit c679f6f

Please sign in to comment.