@@ -17,6 +17,7 @@ error-handling for C++ exceptions and JavaScript exceptions.
17
17
The following sections explain the approach for each case:
18
18
19
19
- [ Handling Errors With C++ Exceptions] ( #exceptions )
20
+ - [ Handling Errors With Maybe Type and C++ Exceptions Disabled] ( #noexceptions-maybe )
20
21
- [ Handling Errors Without C++ Exceptions] ( #noexceptions )
21
22
22
23
<a name =" exceptions " ></a >
@@ -70,7 +71,7 @@ when returning to JavaScript.
70
71
### Propagating a Node-API C++ exception
71
72
72
73
```cpp
73
- Napi::Function jsFunctionThatThrows = someObj .As<Napi::Function>();
74
+ Napi::Function jsFunctionThatThrows = someValue .As<Napi::Function>();
74
75
Napi::Value result = jsFunctionThatThrows({ arg1, arg2 });
75
76
// other C++ statements
76
77
// ...
@@ -84,7 +85,7 @@ a JavaScript exception when returning to JavaScript.
84
85
### Handling a Node-API C++ exception
85
86
86
87
``` cpp
87
- Napi::Function jsFunctionThatThrows = someObj .As<Napi::Function>();
88
+ Napi::Function jsFunctionThatThrows = someValue .As<Napi::Function>();
88
89
Napi::Value result;
89
90
try {
90
91
result = jsFunctionThatThrows({ arg1, arg2 });
96
97
Since the exception was caught here, it will not be propagated as a JavaScript
97
98
exception.
98
99
100
+ <a name =" noexceptions-maybe " ></a >
101
+
102
+ ## Handling Errors With Maybe Type and C++ Exceptions Disabled
103
+
104
+ If C++ exceptions are disabled (for more info see: [ Setup] ( setup.md ) ), then the
105
+ ` Napi::Error ` class does not extend ` std::exception ` . This means that any calls to
106
+ node-addon-api functions do not throw a C++ exceptions. Instead, these node-api
107
+ functions that call into JavaScript are returning with ` Maybe ` boxed values.
108
+ In that case, the calling side should convert the ` Maybe ` boxed values with
109
+ checks to ensure that the call did succeed and therefore no exception is pending.
110
+ If the check fails, that is to say, the returning value is _ empty_ , the calling
111
+ side should determine what to do with ` env.GetAndClearPendingException() ` before
112
+ attempting to call another node-api (for more info see: [ Env] ( env.md ) ).
113
+
114
+ The conversion from the ` Maybe ` boxed value to the actual return value is
115
+ enforced by compilers so that the exceptions must be properly handled before
116
+ continuing.
117
+
118
+ ## Examples with Maybe Type and C++ exceptions disabled
119
+
120
+ ### Throwing a JS exception
121
+
122
+ ``` cpp
123
+ Napi::Env env = ...
124
+ Napi::Error::New (env, "Example exception").ThrowAsJavaScriptException();
125
+ return;
126
+ ```
127
+
128
+ After throwing a JavaScript exception, the code should generally return
129
+ immediately from the native callback, after performing any necessary cleanup.
130
+
131
+ ### Propagating a Node-API JS exception
132
+
133
+ ```cpp
134
+ Napi::Env env = ...
135
+ Napi::Function jsFunctionThatThrows = someValue.As<Napi::Function>();
136
+ Maybe<Napi::Value> maybeResult = jsFunctionThatThrows({ arg1, arg2 });
137
+ Napi::Value result;
138
+ if (!maybeResult.To(&result)) {
139
+ // The Maybe is empty, calling into js failed, cleaning up...
140
+ // It is recommended to return an empty Maybe if the procedure failed.
141
+ return result;
142
+ }
143
+ ```
144
+
145
+ If ` maybeResult.To(&result) ` returns false a JavaScript exception is pending.
146
+ To let the exception propagate, the code should generally return immediately
147
+ from the native callback, after performing any necessary cleanup.
148
+
149
+ ### Handling a Node-API JS exception
150
+
151
+ ``` cpp
152
+ Napi::Env env = ...
153
+ Napi::Function jsFunctionThatThrows = someValue.As<Napi::Function>();
154
+ Maybe<Napi::Value> maybeResult = jsFunctionThatThrows({ arg1, arg2 });
155
+ if (maybeResult.IsNothing()) {
156
+ Napi::Error e = env.GetAndClearPendingException();
157
+ cerr << "Caught JavaScript exception: " + e.Message();
158
+ }
159
+ ```
160
+
161
+ Since the exception was cleared here, it will not be propagated as a JavaScript
162
+ exception after the native callback returns.
163
+
99
164
<a name =" noexceptions " ></a >
100
165
101
166
## Handling Errors Without C++ Exceptions
@@ -127,7 +192,7 @@ immediately from the native callback, after performing any necessary cleanup.
127
192
128
193
```cpp
129
194
Napi::Env env = ...
130
- Napi::Function jsFunctionThatThrows = someObj .As<Napi::Function>();
195
+ Napi::Function jsFunctionThatThrows = someValue .As<Napi::Function>();
131
196
Napi::Value result = jsFunctionThatThrows({ arg1, arg2 });
132
197
if (env.IsExceptionPending()) {
133
198
Error e = env.GetAndClearPendingException();
@@ -143,7 +208,7 @@ the native callback, after performing any necessary cleanup.
143
208
144
209
``` cpp
145
210
Napi::Env env = ...
146
- Napi::Function jsFunctionThatThrows = someObj .As<Napi::Function>();
211
+ Napi::Function jsFunctionThatThrows = someValue .As<Napi::Function>();
147
212
Napi::Value result = jsFunctionThatThrows({ arg1, arg2 });
148
213
if (env.IsExceptionPending()) {
149
214
Napi::Error e = env.GetAndClearPendingException();
0 commit comments