Skip to content

Commit

Permalink
src: reset error struct if error code is napi_ok
Browse files Browse the repository at this point in the history
PR-URL: #40552
Refs: nodejs/node-addon-api#1089
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <midawson@redhat.com>
Reviewed-By: Gabriel Schulhof <gabrielschulhof@gmail.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
  • Loading branch information
JckXia authored and danielleadams committed Jan 30, 2022
1 parent 343be70 commit 6532063
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/js_native_api_v8.cc
Expand Up @@ -761,12 +761,14 @@ napi_status napi_get_last_error_info(napi_env env,
NAPI_ARRAYSIZE(error_messages) == last_status + 1,
"Count of error messages must match count of error values");
CHECK_LE(env->last_error.error_code, last_status);

// Wait until someone requests the last error information to fetch the error
// message string
env->last_error.error_message =
error_messages[env->last_error.error_code];

if (env->last_error.error_code == napi_ok) {
napi_clear_last_error(env);
}
*result = &(env->last_error);
return napi_ok;
}
Expand Down
1 change: 1 addition & 0 deletions src/js_native_api_v8.h
Expand Up @@ -159,6 +159,7 @@ static inline napi_status napi_clear_last_error(napi_env env) {
// TODO(boingoing): Should this be a callback?
env->last_error.engine_error_code = 0;
env->last_error.engine_reserved = nullptr;
env->last_error.error_message = nullptr;
return napi_ok;
}

Expand Down
7 changes: 4 additions & 3 deletions test/js-native-api/common.h
Expand Up @@ -8,12 +8,13 @@
const napi_extended_error_info *error_info; \
napi_get_last_error_info((env), &error_info); \
bool is_pending; \
const char* err_message = error_info->error_message; \
napi_is_exception_pending((env), &is_pending); \
/* If an exception is already pending, don't rethrow it */ \
if (!is_pending) { \
const char* error_message = error_info->error_message != NULL ? \
error_info->error_message : \
"empty error message"; \
const char* error_message = err_message != NULL ? \
err_message : \
"empty error message"; \
napi_throw_error((env), NULL, error_message); \
} \
} while (0)
Expand Down

0 comments on commit 6532063

Please sign in to comment.