Skip to content

Commit 2e1769e

Browse files
legendecasGabriel Schulhof
authored andcommitted
error: remove unnecessary if condition
`NAPI_FATAL_IF_FAILED` would not return if the status is not `napi_ok`. PR-URL: #562 Reviewed-By: NickNaso <[email protected]> Reviewed-By: Gabriel Schulhof <[email protected]>
1 parent 828f223 commit 2e1769e

File tree

1 file changed

+32
-36
lines changed

1 file changed

+32
-36
lines changed

napi-inl.h

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1994,47 +1994,43 @@ inline Error Error::New(napi_env env) {
19941994
status = napi_get_last_error_info(env, &info);
19951995
NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_get_last_error_info");
19961996

1997-
if (status == napi_ok) {
1998-
if (info->error_code == napi_pending_exception) {
1997+
if (info->error_code == napi_pending_exception) {
1998+
status = napi_get_and_clear_last_exception(env, &error);
1999+
NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_get_and_clear_last_exception");
2000+
}
2001+
else {
2002+
const char* error_message = info->error_message != nullptr ?
2003+
info->error_message : "Error in native callback";
2004+
2005+
bool isExceptionPending;
2006+
status = napi_is_exception_pending(env, &isExceptionPending);
2007+
NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_is_exception_pending");
2008+
2009+
if (isExceptionPending) {
19992010
status = napi_get_and_clear_last_exception(env, &error);
20002011
NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_get_and_clear_last_exception");
20012012
}
2002-
else {
2003-
const char* error_message = info->error_message != nullptr ?
2004-
info->error_message : "Error in native callback";
2005-
2006-
bool isExceptionPending;
2007-
status = napi_is_exception_pending(env, &isExceptionPending);
2008-
NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_is_exception_pending");
20092013

2010-
if (isExceptionPending) {
2011-
status = napi_get_and_clear_last_exception(env, &error);
2012-
NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_get_and_clear_last_exception");
2013-
}
2014-
2015-
napi_value message;
2016-
status = napi_create_string_utf8(
2017-
env,
2018-
error_message,
2019-
std::strlen(error_message),
2020-
&message);
2021-
NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_create_string_utf8");
2022-
2023-
if (status == napi_ok) {
2024-
switch (info->error_code) {
2025-
case napi_object_expected:
2026-
case napi_string_expected:
2027-
case napi_boolean_expected:
2028-
case napi_number_expected:
2029-
status = napi_create_type_error(env, nullptr, message, &error);
2030-
break;
2031-
default:
2032-
status = napi_create_error(env, nullptr, message, &error);
2033-
break;
2034-
}
2035-
NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_create_error");
2036-
}
2014+
napi_value message;
2015+
status = napi_create_string_utf8(
2016+
env,
2017+
error_message,
2018+
std::strlen(error_message),
2019+
&message);
2020+
NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_create_string_utf8");
2021+
2022+
switch (info->error_code) {
2023+
case napi_object_expected:
2024+
case napi_string_expected:
2025+
case napi_boolean_expected:
2026+
case napi_number_expected:
2027+
status = napi_create_type_error(env, nullptr, message, &error);
2028+
break;
2029+
default:
2030+
status = napi_create_error(env, nullptr, message, &error);
2031+
break;
20372032
}
2033+
NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_create_error");
20382034
}
20392035

20402036
return Error(env, error);

0 commit comments

Comments
 (0)