-
Notifications
You must be signed in to change notification settings - Fork 459
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add logic to handle graceful error handling when primitive errors are thrown by JS functions #1075
Changes from 1 commit
ed4d1c5
c89f0bf
02bcfbc
173c5bc
a0b3fe9
6918138
e0567d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -2603,20 +2603,45 @@ inline Error::Error(napi_env env, napi_value value) : ObjectReference(env, nullp | |||||||
|
||||||||
NAPI_FATAL_IF_FAILED(status, "Error::Error", "napi_create_object"); | ||||||||
|
||||||||
status = napi_set_property(env, | ||||||||
wrappedErrorObj, | ||||||||
String::From(env, "errorVal"), | ||||||||
Value::From(env, value)); | ||||||||
NAPI_FATAL_IF_FAILED(status, "Error::Error", "napi_set_property"); | ||||||||
|
||||||||
status = napi_set_property( | ||||||||
env, | ||||||||
wrappedErrorObj, | ||||||||
String::From(env, | ||||||||
"4b3d96fd-fb87-4951-a979-eb4f9d2f2ce9-isWrapObject"), | ||||||||
Value::From(env, value)); | ||||||||
NAPI_FATAL_IF_FAILED(status, "Error::Error", "napi_set_property"); | ||||||||
napi_property_descriptor errValDesc = { | ||||||||
"errorVal", // const char* utf8Name | ||||||||
String::From(env, "errorVal"), // napi_value name | ||||||||
nullptr, // Method | ||||||||
nullptr, // getter | ||||||||
nullptr, // setter | ||||||||
Value::From(env, value), // napi_value value | ||||||||
napi_enumerable, | ||||||||
nullptr}; | ||||||||
|
||||||||
status = napi_define_properties(env, wrappedErrorObj, 1, &errValDesc); | ||||||||
NAPI_FATAL_IF_FAILED(status, "Error::Error", "napi_define_properties"); | ||||||||
|
||||||||
MaybeOrValue<Symbol> result = Symbol::For(env, "isWrapObject"); | ||||||||
napi_property_descriptor wrapObjFlag = {nullptr, | ||||||||
nullptr, | ||||||||
nullptr, | ||||||||
nullptr, | ||||||||
nullptr, | ||||||||
Value::From(env, value), | ||||||||
napi_enumerable, | ||||||||
nullptr}; | ||||||||
|
||||||||
#ifdef NODE_ADDON_API_ENABLE_MAYBE | ||||||||
Symbol uniqueSymb; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||
if (result.IsJust()) { | ||||||||
uniqueSymb = result.Unwrap(); | ||||||||
} | ||||||||
|
||||||||
wrapObjFlag.name = uniqueSymb; | ||||||||
status = napi_define_properties(env, wrappedErrorObj, 1, &wrapObjFlag); | ||||||||
NAPI_FATAL_IF_FAILED(status, "Error::Error", "napi_define_properties"); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||
#else | ||||||||
|
||||||||
wrapObjFlag.name = result; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
status = napi_define_properties(env, wrappedErrorObj, 1, &wrapObjFlag); | ||||||||
NAPI_FATAL_IF_FAILED(status, "Error::Error", "napi_define_properties"); | ||||||||
Comment on lines
+2620
to
+2621
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||
|
||||||||
#endif | ||||||||
status = napi_create_reference(env, wrappedErrorObj, 1, &_ref); | ||||||||
} | ||||||||
|
||||||||
|
@@ -2642,18 +2667,31 @@ inline Object Error::Value() const { | |||||||
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); | ||||||||
|
||||||||
MaybeOrValue<Symbol> result = Symbol::For(_env, "isWrapObject"); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One suggestion is maybe we should still use a GUID here instead of the string. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this also be guarded by |
||||||||
|
||||||||
#ifdef NODE_ADDON_API_ENABLE_MAYBE | ||||||||
Symbol uniqueSymb; | ||||||||
if (result.IsJust()) { | ||||||||
uniqueSymb = result.Unwrap(); | ||||||||
} | ||||||||
status = napi_has_property(_env, refValue, uniqueSymb, &isWrappedObject); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use the same pattern here as I mentioned before. Move |
||||||||
NAPI_FATAL_IF_FAILED(status, "Error::Error", "napi_set_property"); | ||||||||
|
||||||||
#else | ||||||||
|
||||||||
status = napi_has_property(_env, refValue, result, &isWrappedObject); | ||||||||
NAPI_FATAL_IF_FAILED(status, "Error::Error", "napi_set_property"); | ||||||||
#endif | ||||||||
|
||||||||
// 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); | ||||||||
} | ||||||||
} | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be
nullptr
.utf8name
is sufficient for specifying the name of the property.