Skip to content

Commit

Permalink
error: remove unnecessary if condition
Browse files Browse the repository at this point in the history
`NAPI_FATAL_IF_FAILED` would not return if the status is not `napi_ok`.

PR-URL: nodejs/node-addon-api#562
Reviewed-By: NickNaso <[email protected]>
Reviewed-By: Gabriel Schulhof <[email protected]>
  • Loading branch information
John French committed Oct 21, 2019
1 parent 59fda26 commit 213188b
Showing 1 changed file with 32 additions and 36 deletions.
68 changes: 32 additions & 36 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1994,47 +1994,43 @@ inline Error Error::New(napi_env env) {
status = napi_get_last_error_info(env, &info);
NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_get_last_error_info");

if (status == napi_ok) {
if (info->error_code == napi_pending_exception) {
if (info->error_code == napi_pending_exception) {
status = napi_get_and_clear_last_exception(env, &error);
NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_get_and_clear_last_exception");
}
else {
const char* error_message = info->error_message != nullptr ?
info->error_message : "Error in native callback";

bool isExceptionPending;
status = napi_is_exception_pending(env, &isExceptionPending);
NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_is_exception_pending");

if (isExceptionPending) {
status = napi_get_and_clear_last_exception(env, &error);
NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_get_and_clear_last_exception");
}
else {
const char* error_message = info->error_message != nullptr ?
info->error_message : "Error in native callback";

bool isExceptionPending;
status = napi_is_exception_pending(env, &isExceptionPending);
NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_is_exception_pending");

if (isExceptionPending) {
status = napi_get_and_clear_last_exception(env, &error);
NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_get_and_clear_last_exception");
}

napi_value message;
status = napi_create_string_utf8(
env,
error_message,
std::strlen(error_message),
&message);
NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_create_string_utf8");

if (status == napi_ok) {
switch (info->error_code) {
case napi_object_expected:
case napi_string_expected:
case napi_boolean_expected:
case napi_number_expected:
status = napi_create_type_error(env, nullptr, message, &error);
break;
default:
status = napi_create_error(env, nullptr, message, &error);
break;
}
NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_create_error");
}
napi_value message;
status = napi_create_string_utf8(
env,
error_message,
std::strlen(error_message),
&message);
NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_create_string_utf8");

switch (info->error_code) {
case napi_object_expected:
case napi_string_expected:
case napi_boolean_expected:
case napi_number_expected:
status = napi_create_type_error(env, nullptr, message, &error);
break;
default:
status = napi_create_error(env, nullptr, message, &error);
break;
}
NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_create_error");
}

return Error(env, error);
Expand Down

0 comments on commit 213188b

Please sign in to comment.