@@ -2593,60 +2593,38 @@ inline Error::Error() : ObjectReference() {
2593
2593
2594
2594
inline Error::Error (napi_env env, napi_value value) : ObjectReference(env, nullptr ) {
2595
2595
if (value != nullptr ) {
2596
+ // Attempting to create a reference on the error object.
2597
+ // If it's not a Object/Function/Symbol, this call will return an error
2598
+ // status.
2596
2599
napi_status status = napi_create_reference (env, value, 1 , &_ref);
2597
2600
2598
- // Creates a wrapper object containg the error value (primitive types) and
2599
- // create a reference to this wrapper
2600
2601
if (status != napi_ok) {
2601
2602
napi_value wrappedErrorObj;
2602
- status = napi_create_object (env, &wrappedErrorObj);
2603
2603
2604
+ // Create an error object
2605
+ status = napi_create_object (env, &wrappedErrorObj);
2604
2606
NAPI_FATAL_IF_FAILED (status, " Error::Error" , " napi_create_object" );
2605
2607
2606
- napi_property_descriptor errValDesc = {
2607
- " errorVal" , // const char* utf8Name
2608
- String::From (env, " errorVal" ), // napi_value name
2609
- nullptr , // Method
2610
- nullptr , // getter
2611
- nullptr , // setter
2612
- Value::From (env, value), // napi_value value
2608
+ // property flag that we attach to show the error object is wrapped
2609
+ napi_property_descriptor wrapObjFlag = {
2610
+ ERROR_WRAP_VALUE, // Unique GUID identifier since Symbol isn't a
2611
+ // viable option
2612
+ nullptr ,
2613
+ nullptr ,
2614
+ nullptr ,
2615
+ nullptr ,
2616
+ Value::From (env, value),
2613
2617
napi_enumerable,
2614
2618
nullptr };
2615
2619
2616
- status = napi_define_properties (env, wrappedErrorObj, 1 , &errValDesc);
2617
- NAPI_FATAL_IF_FAILED (status, " Error::Error" , " napi_define_properties" );
2618
-
2619
- MaybeOrValue<Symbol> result = Symbol::For (env, " isWrapObject" );
2620
- napi_property_descriptor wrapObjFlag = {nullptr ,
2621
- nullptr ,
2622
- nullptr ,
2623
- nullptr ,
2624
- nullptr ,
2625
- Value::From (env, value),
2626
- napi_enumerable,
2627
- nullptr };
2628
-
2629
- #ifdef NODE_ADDON_API_ENABLE_MAYBE
2630
- Symbol uniqueSymb;
2631
- if (result.IsJust ()) {
2632
- uniqueSymb = result.Unwrap ();
2633
- }
2634
-
2635
- wrapObjFlag.name = uniqueSymb;
2636
2620
status = napi_define_properties (env, wrappedErrorObj, 1 , &wrapObjFlag);
2637
2621
NAPI_FATAL_IF_FAILED (status, " Error::Error" , " napi_define_properties" );
2638
- #else
2639
2622
2640
- wrapObjFlag.name = result;
2641
- status = napi_define_properties (env, wrappedErrorObj, 1 , &wrapObjFlag);
2642
- NAPI_FATAL_IF_FAILED (status, " Error::Error" , " napi_define_properties" );
2643
-
2644
- #endif
2623
+ // Create a reference on the newly wrapped object
2645
2624
status = napi_create_reference (env, wrappedErrorObj, 1 , &_ref);
2646
2625
}
2647
2626
2648
2627
// Avoid infinite recursion in the failure case.
2649
- // Don't try to construct & throw another Error instance.
2650
2628
NAPI_FATAL_IF_FAILED (status, " Error::Error" , " napi_create_reference" );
2651
2629
}
2652
2630
}
@@ -2664,32 +2642,22 @@ inline Object Error::Value() const {
2664
2642
status = napi_typeof (_env, refValue, &type);
2665
2643
NAPI_THROW_IF_FAILED (_env, status, Object ());
2666
2644
2645
+ // If refValue isn't a symbol, then we proceed to whether the refValue has the
2646
+ // wrapped error flag
2667
2647
if (type != napi_symbol) {
2668
2648
// We are checking if the object is wrapped
2669
2649
bool isWrappedObject = false ;
2670
2650
2671
- MaybeOrValue<Symbol> result = Symbol::For (_env, " isWrapObject" );
2672
-
2673
- #ifdef NODE_ADDON_API_ENABLE_MAYBE
2674
- Symbol uniqueSymb;
2675
- if (result.IsJust ()) {
2676
- uniqueSymb = result.Unwrap ();
2677
- }
2678
- status = napi_has_property (_env, refValue, uniqueSymb, &isWrappedObject);
2679
- NAPI_FATAL_IF_FAILED (status, " Error::Error" , " napi_set_property" );
2680
-
2681
- #else
2682
-
2683
- status = napi_has_property (_env, refValue, result, &isWrappedObject);
2684
- NAPI_FATAL_IF_FAILED (status, " Error::Error" , " napi_set_property" );
2685
- #endif
2651
+ status = napi_has_property (
2652
+ _env, refValue, String::From (_env, ERROR_WRAP_VALUE), &isWrappedObject);
2686
2653
2687
2654
// Don't care about status
2688
-
2689
- if (isWrappedObject == true ) {
2655
+ if (isWrappedObject) {
2690
2656
napi_value unwrappedValue;
2691
- status = napi_get_property (
2692
- _env, refValue, String::From (_env, " errorVal" ), &unwrappedValue);
2657
+ status = napi_get_property (_env,
2658
+ refValue,
2659
+ String::From (_env, ERROR_WRAP_VALUE),
2660
+ &unwrappedValue);
2693
2661
NAPI_THROW_IF_FAILED (_env, status, Object ());
2694
2662
2695
2663
return Object (_env, unwrappedValue);
0 commit comments