Skip to content

Commit

Permalink
n-api: Retain last code when getting error info
Browse files Browse the repository at this point in the history
Unlike most N-API functions, `napi_get_last_error_info()` should not
clear the last error code when successful, because a pointer to (not
a copy of) the error info structure is returned via an out parameter.

Backport-PR-URL: #19447
PR-URL: #13087
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
  • Loading branch information
jasongin authored and MylesBorins committed Apr 16, 2018
1 parent 71aa251 commit 70281ba
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/node_api.cc
Expand Up @@ -757,7 +757,7 @@ napi_status napi_get_last_error_info(napi_env env,
error_messages[env->last_error.error_code];

*result = &(env->last_error);
return napi_clear_last_error(env);
return napi_ok;
}

napi_status napi_create_function(napi_env env,
Expand Down
8 changes: 8 additions & 0 deletions test/addons-napi/test_napi_status/test_napi_status.cc
Expand Up @@ -10,6 +10,14 @@ napi_value createNapiError(napi_env env, napi_callback_info info) {

NAPI_ASSERT(env, status != napi_ok, "Failed to produce error condition");

const napi_extended_error_info *error_info = 0;
NAPI_CALL(env, napi_get_last_error_info(env, &error_info));

NAPI_ASSERT(env, error_info->error_code == status,
"Last error info code should match last status");
NAPI_ASSERT(env, error_info->error_message,
"Last error info message should not be null");

return nullptr;
}

Expand Down

0 comments on commit 70281ba

Please sign in to comment.