@@ -2593,14 +2593,80 @@ 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
2601
+ if (status != napi_ok) {
2602
+ napi_value wrappedErrorObj;
2603
+
2604
+ // Create an error object
2605
+ status = napi_create_object (env, &wrappedErrorObj);
2606
+ NAPI_FATAL_IF_FAILED (status, " Error::Error" , " napi_create_object" );
2607
+
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),
2617
+ napi_enumerable,
2618
+ nullptr };
2619
+
2620
+ status = napi_define_properties (env, wrappedErrorObj, 1 , &wrapObjFlag);
2621
+ NAPI_FATAL_IF_FAILED (status, " Error::Error" , " napi_define_properties" );
2622
+
2623
+ // Create a reference on the newly wrapped object
2624
+ status = napi_create_reference (env, wrappedErrorObj, 1 , &_ref);
2625
+ }
2626
+
2598
2627
// Avoid infinite recursion in the failure case.
2599
- // Don't try to construct & throw another Error instance.
2600
2628
NAPI_FATAL_IF_FAILED (status, " Error::Error" , " napi_create_reference" );
2601
2629
}
2602
2630
}
2603
2631
2632
+ inline Object Error::Value () const {
2633
+ if (_ref == nullptr ) {
2634
+ return Object (_env, nullptr );
2635
+ }
2636
+
2637
+ napi_value refValue;
2638
+ napi_status status = napi_get_reference_value (_env, _ref, &refValue);
2639
+ NAPI_THROW_IF_FAILED (_env, status, Object ());
2640
+
2641
+ napi_valuetype type;
2642
+ status = napi_typeof (_env, refValue, &type);
2643
+ NAPI_THROW_IF_FAILED (_env, status, Object ());
2644
+
2645
+ // If refValue isn't a symbol, then we proceed to whether the refValue has the
2646
+ // wrapped error flag
2647
+ if (type != napi_symbol) {
2648
+ // We are checking if the object is wrapped
2649
+ bool isWrappedObject = false ;
2650
+
2651
+ status = napi_has_property (
2652
+ _env, refValue, String::From (_env, ERROR_WRAP_VALUE), &isWrappedObject);
2653
+
2654
+ // Don't care about status
2655
+ if (isWrappedObject) {
2656
+ napi_value unwrappedValue;
2657
+ status = napi_get_property (_env,
2658
+ refValue,
2659
+ String::From (_env, ERROR_WRAP_VALUE),
2660
+ &unwrappedValue);
2661
+ NAPI_THROW_IF_FAILED (_env, status, Object ());
2662
+
2663
+ return Object (_env, unwrappedValue);
2664
+ }
2665
+ }
2666
+
2667
+ return Object (_env, refValue);
2668
+ }
2669
+
2604
2670
inline Error::Error (Error&& other) : ObjectReference(std::move(other)) {
2605
2671
}
2606
2672
@@ -2651,6 +2717,7 @@ inline const std::string& Error::Message() const NAPI_NOEXCEPT {
2651
2717
return _message;
2652
2718
}
2653
2719
2720
+ // we created an object on the &_ref
2654
2721
inline void Error::ThrowAsJavaScriptException () const {
2655
2722
HandleScope scope (_env);
2656
2723
if (!IsEmpty ()) {
0 commit comments