@@ -2520,12 +2520,23 @@ inline Error Error::New(napi_env env) {
2520
2520
napi_status status;
2521
2521
napi_value error = nullptr ;
2522
2522
bool is_exception_pending;
2523
- const napi_extended_error_info* info ;
2523
+ napi_extended_error_info last_error_info_copy ;
2524
2524
2525
- // We must retrieve the last error info before doing anything else, because
2526
- // doing anything else will replace the last error info.
2527
- status = napi_get_last_error_info (env, &info);
2528
- NAPI_FATAL_IF_FAILED (status, " Error::New" , " napi_get_last_error_info" );
2525
+ {
2526
+ // We must retrieve the last error info before doing anything else because
2527
+ // doing anything else will replace the last error info.
2528
+ const napi_extended_error_info* last_error_info;
2529
+ status = napi_get_last_error_info (env, &last_error_info);
2530
+ NAPI_FATAL_IF_FAILED (status, " Error::New" , " napi_get_last_error_info" );
2531
+
2532
+ // All fields of the `napi_extended_error_info` structure gets reset in
2533
+ // subsequent Node-API function calls on the same `env`. This includes a
2534
+ // call to `napi_is_exception_pending()`. So here it is necessary to make a
2535
+ // copy of the information as the `error_code` field is used later on.
2536
+ memcpy (&last_error_info_copy,
2537
+ last_error_info,
2538
+ sizeof (napi_extended_error_info));
2539
+ }
2529
2540
2530
2541
status = napi_is_exception_pending (env, &is_exception_pending);
2531
2542
NAPI_FATAL_IF_FAILED (status, " Error::New" , " napi_is_exception_pending" );
@@ -2536,8 +2547,9 @@ inline Error Error::New(napi_env env) {
2536
2547
NAPI_FATAL_IF_FAILED (status, " Error::New" , " napi_get_and_clear_last_exception" );
2537
2548
}
2538
2549
else {
2539
- const char * error_message = info->error_message != nullptr ?
2540
- info->error_message : " Error in native callback" ;
2550
+ const char * error_message = last_error_info_copy.error_message != nullptr
2551
+ ? last_error_info_copy.error_message
2552
+ : " Error in native callback" ;
2541
2553
2542
2554
napi_value message;
2543
2555
status = napi_create_string_utf8 (
@@ -2547,16 +2559,16 @@ inline Error Error::New(napi_env env) {
2547
2559
&message);
2548
2560
NAPI_FATAL_IF_FAILED (status, " Error::New" , " napi_create_string_utf8" );
2549
2561
2550
- switch (info-> error_code ) {
2551
- case napi_object_expected:
2552
- case napi_string_expected:
2553
- case napi_boolean_expected:
2554
- case napi_number_expected:
2555
- status = napi_create_type_error (env, nullptr , message, &error);
2556
- break ;
2557
- default :
2558
- status = napi_create_error (env, nullptr , message, &error);
2559
- break ;
2562
+ switch (last_error_info_copy. error_code ) {
2563
+ case napi_object_expected:
2564
+ case napi_string_expected:
2565
+ case napi_boolean_expected:
2566
+ case napi_number_expected:
2567
+ status = napi_create_type_error (env, nullptr , message, &error);
2568
+ break ;
2569
+ default :
2570
+ status = napi_create_error (env, nullptr , message, &error);
2571
+ break ;
2560
2572
}
2561
2573
NAPI_FATAL_IF_FAILED (status, " Error::New" , " napi_create_error" );
2562
2574
}
0 commit comments