@@ -17,6 +17,7 @@ error-handling for C++ exceptions and JavaScript exceptions.
1717The following sections explain the approach for each case:
1818
1919- [ Handling Errors With C++ Exceptions] ( #exceptions )
20+ - [ Handling Errors With Maybe Type and C++ Exceptions Disabled] ( #noexceptions-maybe )
2021- [ Handling Errors Without C++ Exceptions] ( #noexceptions )
2122
2223<a name =" exceptions " ></a >
@@ -70,7 +71,7 @@ when returning to JavaScript.
7071### Propagating a Node-API C++ exception
7172
7273```cpp
73- Napi::Function jsFunctionThatThrows = someObj .As<Napi::Function>();
74+ Napi::Function jsFunctionThatThrows = someValue .As<Napi::Function>();
7475Napi::Value result = jsFunctionThatThrows({ arg1, arg2 });
7576// other C++ statements
7677// ...
@@ -84,7 +85,7 @@ a JavaScript exception when returning to JavaScript.
8485### Handling a Node-API C++ exception
8586
8687``` cpp
87- Napi::Function jsFunctionThatThrows = someObj .As<Napi::Function>();
88+ Napi::Function jsFunctionThatThrows = someValue .As<Napi::Function>();
8889Napi::Value result;
8990try {
9091 result = jsFunctionThatThrows({ arg1, arg2 });
9697Since the exception was caught here, it will not be propagated as a JavaScript
9798exception.
9899
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+
99164<a name =" noexceptions " ></a >
100165
101166## Handling Errors Without C++ Exceptions
@@ -127,7 +192,7 @@ immediately from the native callback, after performing any necessary cleanup.
127192
128193```cpp
129194Napi::Env env = ...
130- Napi::Function jsFunctionThatThrows = someObj .As<Napi::Function>();
195+ Napi::Function jsFunctionThatThrows = someValue .As<Napi::Function>();
131196Napi::Value result = jsFunctionThatThrows({ arg1, arg2 });
132197if (env.IsExceptionPending()) {
133198 Error e = env.GetAndClearPendingException();
@@ -143,7 +208,7 @@ the native callback, after performing any necessary cleanup.
143208
144209``` cpp
145210Napi::Env env = ...
146- Napi::Function jsFunctionThatThrows = someObj .As<Napi::Function>();
211+ Napi::Function jsFunctionThatThrows = someValue .As<Napi::Function>();
147212Napi::Value result = jsFunctionThatThrows({ arg1, arg2 });
148213if (env.IsExceptionPending()) {
149214 Napi::Error e = env.GetAndClearPendingException();
0 commit comments