99
1010// Note: Do not include this file directly! Include "napi.h" instead.
1111
12- #include < cassert>
1312#include < cstring>
1413
1514namespace Napi {
@@ -42,6 +41,13 @@ namespace details {
4241
4342#endif // NAPI_CPP_EXCEPTIONS
4443
44+ #define NAPI_FATAL_IF_FAILED (status, location, message ) \
45+ do { \
46+ if ((status) != napi_ok) { \
47+ Error::Fatal ((location), (message)); \
48+ } \
49+ } while (0 )
50+
4551// For use in JS to C++ callback wrappers to catch any Napi::Error exceptions
4652// and rethrow them as JavaScript exceptions before returning from the callback.
4753template <typename Callable>
@@ -1418,24 +1424,24 @@ inline Error Error::New(napi_env env) {
14181424
14191425 const napi_extended_error_info* info;
14201426 status = napi_get_last_error_info (env, &info);
1421- assert (status == napi_ok );
1427+ NAPI_FATAL_IF_FAILED (status, " Error::New " , " napi_get_last_error_info " );
14221428
14231429 if (status == napi_ok) {
14241430 if (info->error_code == napi_pending_exception) {
14251431 status = napi_get_and_clear_last_exception (env, &error);
1426- assert (status == napi_ok );
1432+ NAPI_FATAL_IF_FAILED (status, " Error::New " , " napi_get_and_clear_last_exception " );
14271433 }
14281434 else {
14291435 const char * error_message = info->error_message != nullptr ?
14301436 info->error_message : " Error in native callback" ;
14311437
14321438 bool isExceptionPending;
14331439 status = napi_is_exception_pending (env, &isExceptionPending);
1434- assert (status == napi_ok );
1440+ NAPI_FATAL_IF_FAILED (status, " Error::New " , " napi_is_exception_pending " );
14351441
14361442 if (isExceptionPending) {
14371443 status = napi_get_and_clear_last_exception (env, &error);
1438- assert (status == napi_ok );
1444+ NAPI_FATAL_IF_FAILED (status, " Error::New " , " napi_get_and_clear_last_exception " );
14391445 }
14401446
14411447 napi_value message;
@@ -1444,7 +1450,7 @@ inline Error Error::New(napi_env env) {
14441450 error_message,
14451451 std::strlen (error_message),
14461452 &message);
1447- assert (status == napi_ok );
1453+ NAPI_FATAL_IF_FAILED (status, " Error::New " , " napi_create_string_utf8 " );
14481454
14491455 if (status == napi_ok) {
14501456 switch (info->error_code ) {
@@ -1458,7 +1464,7 @@ inline Error Error::New(napi_env env) {
14581464 status = napi_create_error (env, nullptr , message, &error);
14591465 break ;
14601466 }
1461- assert (status == napi_ok );
1467+ NAPI_FATAL_IF_FAILED (status, " Error::New " , " napi_create_error " );
14621468 }
14631469 }
14641470 }
@@ -1474,6 +1480,10 @@ inline Error Error::New(napi_env env, const std::string& message) {
14741480 return Error::New<Error>(env, message.c_str (), message.size (), napi_create_error);
14751481}
14761482
1483+ inline NAPI_NO_RETURN void Error::Fatal (const char * location, const char * message) {
1484+ napi_fatal_error (location, message);
1485+ }
1486+
14771487inline Error::Error () : ObjectReference(), _message(nullptr ) {
14781488}
14791489
@@ -1483,7 +1493,7 @@ inline Error::Error(napi_env env, napi_value value) : ObjectReference(env, nullp
14831493
14841494 // Avoid infinite recursion in the failure case.
14851495 // Don't try to construct & throw another Error instance.
1486- assert (status == napi_ok );
1496+ NAPI_FATAL_IF_FAILED (status, " Error::Error " , " napi_create_reference " );
14871497 }
14881498}
14891499
@@ -1661,9 +1671,7 @@ inline Reference<T>::Reference(const Reference<T>& other)
16611671 // Copying is a limited scenario (currently only used for Error object) and always creates a
16621672 // strong reference to the given value even if the incoming reference is weak.
16631673 napi_status status = napi_create_reference (_env, value, 1 , &_ref);
1664-
1665- // TODO - Switch to napi_fatal_error() once it exists.
1666- assert (status == napi_ok);
1674+ NAPI_FATAL_IF_FAILED (status, " Reference<T>::Reference" , " napi_create_reference" );
16671675 }
16681676}
16691677
0 commit comments