@@ -2088,28 +2088,26 @@ inline void Buffer<T>::EnsureInfo() const {
20882088inline Error Error::New (napi_env env) {
20892089 napi_status status;
20902090 napi_value error = nullptr ;
2091-
2091+ bool is_exception_pending;
20922092 const napi_extended_error_info* info;
2093+
2094+ // We must retrieve the last error info before doing anything else, because
2095+ // doing anything else will replace the last error info.
20932096 status = napi_get_last_error_info (env, &info);
20942097 NAPI_FATAL_IF_FAILED (status, " Error::New" , " napi_get_last_error_info" );
20952098
2096- if (info->error_code == napi_pending_exception) {
2099+ status = napi_is_exception_pending (env, &is_exception_pending);
2100+ NAPI_FATAL_IF_FAILED (status, " Error::New" , " napi_is_exception_pending" );
2101+
2102+ // A pending exception takes precedence over any internal error status.
2103+ if (is_exception_pending) {
20972104 status = napi_get_and_clear_last_exception (env, &error);
20982105 NAPI_FATAL_IF_FAILED (status, " Error::New" , " napi_get_and_clear_last_exception" );
20992106 }
21002107 else {
21012108 const char * error_message = info->error_message != nullptr ?
21022109 info->error_message : " Error in native callback" ;
21032110
2104- bool isExceptionPending;
2105- status = napi_is_exception_pending (env, &isExceptionPending);
2106- NAPI_FATAL_IF_FAILED (status, " Error::New" , " napi_is_exception_pending" );
2107-
2108- if (isExceptionPending) {
2109- status = napi_get_and_clear_last_exception (env, &error);
2110- NAPI_FATAL_IF_FAILED (status, " Error::New" , " napi_get_and_clear_last_exception" );
2111- }
2112-
21132111 napi_value message;
21142112 status = napi_create_string_utf8 (
21152113 env,
0 commit comments