diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index 26ec837ffdcb43..56bd677b76ba89 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -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; } diff --git a/src/js_native_api_v8.h b/src/js_native_api_v8.h index 91c2eeb98989ca..673e697dd6ba1b 100644 --- a/src/js_native_api_v8.h +++ b/src/js_native_api_v8.h @@ -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; } diff --git a/test/js-native-api/common.h b/test/js-native-api/common.h index ab6785fb199b01..73f60906630140 100644 --- a/test/js-native-api/common.h +++ b/test/js-native-api/common.h @@ -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)