Skip to content

Commit 17bff94

Browse files
committed
Update PR based on review comments
1 parent a8e4119 commit 17bff94

6 files changed

+26
-20
lines changed

doc/error_handling.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ If C++ exceptions are enabled (for more info see: [Setup](setup.md)), then the
1414
`Napi::Error` class extends `std::exception` and enables integrated
1515
error-handling for C++ exceptions and JavaScript exceptions.
1616

17-
Note, that due to limitations of the N-API, if one attempts to cast the error object wrapping a primitive inside a C++ addon, the wrapped object
18-
will be received instead. (With properties ```4b3d96fd-fb87-4951-a979-eb4f9d2f2ce9-isWrapObject``` and ```errorVal``` containing the primitive value thrown)
19-
17+
Note, that due to limitations of the Node-API, if one attempts to cast the error object wrapping a primitive inside a C++ addon, the wrapped object
18+
will be received instead. (With properties `4b3d96fd-fb87-4951-a979-eb4f9d2f2ce9-isWrapObject` and `errorVal` containing the primitive value thrown)
2019

2120
The following sections explain the approach for each case:
2221

napi-inl.h

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2623,21 +2623,27 @@ inline Object Error::Value() const {
26232623
napi_status status = napi_get_reference_value(_env, _ref, &refValue);
26242624
NAPI_THROW_IF_FAILED(_env, status, Object());
26252625

2626-
// We are checking if the object is wrapped
2627-
bool isWrappedObject = false;
2628-
napi_has_property(
2629-
_env,
2630-
refValue,
2631-
String::From(_env, "4b3d96fd-fb87-4951-a979-eb4f9d2f2ce9-isWrapObject"),
2632-
&isWrappedObject);
2633-
// Don't care about status
2634-
2635-
if (isWrappedObject == true) {
2636-
napi_value unwrappedValue;
2637-
status = napi_get_property(
2638-
_env, refValue, String::From(_env, "errorVal"), &unwrappedValue);
2639-
NAPI_THROW_IF_FAILED(_env, status, Object());
2640-
return Object(_env, unwrappedValue);
2626+
napi_valuetype type;
2627+
status = napi_typeof(_env, refValue, &type);
2628+
NAPI_THROW_IF_FAILED(_env, status, Object());
2629+
2630+
if (type != napi_symbol) {
2631+
// We are checking if the object is wrapped
2632+
bool isWrappedObject = false;
2633+
napi_has_property(
2634+
_env,
2635+
refValue,
2636+
String::From(_env, "4b3d96fd-fb87-4951-a979-eb4f9d2f2ce9-isWrapObject"),
2637+
&isWrappedObject);
2638+
// Don't care about status
2639+
2640+
if (isWrappedObject == true) {
2641+
napi_value unwrappedValue;
2642+
status = napi_get_property(
2643+
_env, refValue, String::From(_env, "errorVal"), &unwrappedValue);
2644+
NAPI_THROW_IF_FAILED(_env, status, Object());
2645+
return Object(_env, unwrappedValue);
2646+
}
26412647
}
26422648

26432649
return Object(_env, refValue);

napi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <mutex>
99
#include <string>
1010
#include <vector>
11+
1112
// VS2015 RTM has bugs with constexpr, so require min of VS2015 Update 3 (known good version)
1213
#if !defined(_MSC_VER) || _MSC_FULL_VER >= 190024210
1314
#define NAPI_HAS_CONSTEXPR 1

test/binding.gyp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
'dataview/dataview_read_write.cc',
2626
'env_cleanup.cc',
2727
'error.cc',
28-
'errorHandlingForPrimitives.cc',
28+
'error_handling_for_primitives.cc',
2929
'external.cc',
3030
'function.cc',
3131
'function_reference.cc',

test/errorHandlingForPrimitives.cc renamed to test/error_handling_for_primitives.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ Napi::Object InitErrorHandlingPrim(Napi::Env env) {
1010
Napi::Object exports = Napi::Object::New(env);
1111
exports.Set("errorHandlingPrim", Napi::Function::New<Test>(env));
1212
return exports;
13-
}
13+
}

0 commit comments

Comments
 (0)