Skip to content

Commit aa47c9f

Browse files
mhdawsonBethGriggs
authored andcommitted
doc: clarify behavior of napi_extended_error_info
Fix up example and make it more explicit on how you need to use napi_extended_error_info in order to help people avoid what might be a common mistake that we made in node-addon-api. Refs: nodejs/node-addon-api#1089 Signed-off-by: Michael Dawson <[email protected]> PR-URL: #40458 Reviewed-By: Gabriel Schulhof <[email protected]> Reviewed-By: Darshan Sen <[email protected]>
1 parent 3ac99a2 commit aa47c9f

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

doc/api/n-api.md

+9-3
Original file line numberDiff line numberDiff line change
@@ -403,12 +403,13 @@ napi_value create_addon(napi_env env);
403403
if (status != napi_ok) { \
404404
const napi_extended_error_info* error_info = NULL; \
405405
napi_get_last_error_info((env), &error_info); \
406+
const char* err_message = error_info->error_message; \
406407
bool is_pending; \
407408
napi_is_exception_pending((env), &is_pending); \
408409
if (!is_pending) { \
409-
const char* message = (error_info->error_message == NULL) \
410+
const char* message = (err_message == NULL) \
410411
? "empty error message" \
411-
: error_info->error_message; \
412+
: err_message; \
412413
napi_throw_error((env), NULL, message); \
413414
return NULL; \
414415
} \
@@ -1005,7 +1006,12 @@ This API retrieves a `napi_extended_error_info` structure with information
10051006
about the last error that occurred.
10061007

10071008
The content of the `napi_extended_error_info` returned is only valid up until
1008-
a Node-API function is called on the same `env`.
1009+
a Node-API function is called on the same `env`. This includes a call to
1010+
`napi_is_exception_pending` so it may often be necessary to make a copy
1011+
of the information so that it can be used later. The pointer returned
1012+
in error_message points to a statically defined string so it is safe to use
1013+
that pointer if you have copied it out of the error_message field (which will
1014+
be overwritten) before another Node-API function was called.
10091015

10101016
Do not rely on the content or format of any of the extended information as it
10111017
is not subject to SemVer and may change at any time. It is intended only for

0 commit comments

Comments
 (0)