Skip to content

Commit

Permalink
Update PR based on review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
JckXia committed Oct 23, 2021
1 parent 02bcfbc commit 173c5bc
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 20 deletions.
5 changes: 2 additions & 3 deletions doc/error_handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ If C++ exceptions are enabled (for more info see: [Setup](setup.md)), then the
`Napi::Error` class extends `std::exception` and enables integrated
error-handling for C++ exceptions and JavaScript exceptions.

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
will be received instead. (With properties ```4b3d96fd-fb87-4951-a979-eb4f9d2f2ce9-isWrapObject``` and ```errorVal``` containing the primitive value thrown)

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
will be received instead. (With properties `4b3d96fd-fb87-4951-a979-eb4f9d2f2ce9-isWrapObject` and `errorVal` containing the primitive value thrown)

The following sections explain the approach for each case:

Expand Down
36 changes: 21 additions & 15 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2635,21 +2635,27 @@ inline Object Error::Value() const {
napi_status status = napi_get_reference_value(_env, _ref, &refValue);
NAPI_THROW_IF_FAILED(_env, status, Object());

// We are checking if the object is wrapped
bool isWrappedObject = false;
napi_has_property(
_env,
refValue,
String::From(_env, "4b3d96fd-fb87-4951-a979-eb4f9d2f2ce9-isWrapObject"),
&isWrappedObject);
// Don't care about status

if (isWrappedObject == true) {
napi_value unwrappedValue;
status = napi_get_property(
_env, refValue, String::From(_env, "errorVal"), &unwrappedValue);
NAPI_THROW_IF_FAILED(_env, status, Object());
return Object(_env, unwrappedValue);
napi_valuetype type;
status = napi_typeof(_env, refValue, &type);
NAPI_THROW_IF_FAILED(_env, status, Object());

if (type != napi_symbol) {
// We are checking if the object is wrapped
bool isWrappedObject = false;
napi_has_property(
_env,
refValue,
String::From(_env, "4b3d96fd-fb87-4951-a979-eb4f9d2f2ce9-isWrapObject"),
&isWrappedObject);
// Don't care about status

if (isWrappedObject == true) {
napi_value unwrappedValue;
status = napi_get_property(
_env, refValue, String::From(_env, "errorVal"), &unwrappedValue);
NAPI_THROW_IF_FAILED(_env, status, Object());
return Object(_env, unwrappedValue);
}
}

return Object(_env, refValue);
Expand Down
1 change: 1 addition & 0 deletions napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <mutex>
#include <string>
#include <vector>

// VS2015 RTM has bugs with constexpr, so require min of VS2015 Update 3 (known good version)
#if !defined(_MSC_VER) || _MSC_FULL_VER >= 190024210
#define NAPI_HAS_CONSTEXPR 1
Expand Down
2 changes: 1 addition & 1 deletion test/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
'dataview/dataview_read_write.cc',
'env_cleanup.cc',
'error.cc',
'errorHandlingForPrimitives.cc',
'error_handling_for_primitives.cc',
'external.cc',
'function.cc',
'function_reference.cc',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ Napi::Object InitErrorHandlingPrim(Napi::Env env) {
Napi::Object exports = Napi::Object::New(env);
exports.Set("errorHandlingPrim", Napi::Function::New<Test>(env));
return exports;
}
}
File renamed without changes.

0 comments on commit 173c5bc

Please sign in to comment.