9
9
10
10
// Note: Do not include this file directly! Include "napi.h" instead.
11
11
12
- #include < cassert>
13
12
#include < cstring>
14
13
15
14
namespace Napi {
@@ -42,6 +41,13 @@ namespace details {
42
41
43
42
#endif // NAPI_CPP_EXCEPTIONS
44
43
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
+
45
51
// For use in JS to C++ callback wrappers to catch any Napi::Error exceptions
46
52
// and rethrow them as JavaScript exceptions before returning from the callback.
47
53
template <typename Callable>
@@ -1418,24 +1424,24 @@ inline Error Error::New(napi_env env) {
1418
1424
1419
1425
const napi_extended_error_info* info;
1420
1426
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 " );
1422
1428
1423
1429
if (status == napi_ok) {
1424
1430
if (info->error_code == napi_pending_exception) {
1425
1431
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 " );
1427
1433
}
1428
1434
else {
1429
1435
const char * error_message = info->error_message != nullptr ?
1430
1436
info->error_message : " Error in native callback" ;
1431
1437
1432
1438
bool isExceptionPending;
1433
1439
status = napi_is_exception_pending (env, &isExceptionPending);
1434
- assert (status == napi_ok );
1440
+ NAPI_FATAL_IF_FAILED (status, " Error::New " , " napi_is_exception_pending " );
1435
1441
1436
1442
if (isExceptionPending) {
1437
1443
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 " );
1439
1445
}
1440
1446
1441
1447
napi_value message;
@@ -1444,7 +1450,7 @@ inline Error Error::New(napi_env env) {
1444
1450
error_message,
1445
1451
std::strlen (error_message),
1446
1452
&message);
1447
- assert (status == napi_ok );
1453
+ NAPI_FATAL_IF_FAILED (status, " Error::New " , " napi_create_string_utf8 " );
1448
1454
1449
1455
if (status == napi_ok) {
1450
1456
switch (info->error_code ) {
@@ -1458,7 +1464,7 @@ inline Error Error::New(napi_env env) {
1458
1464
status = napi_create_error (env, nullptr , message, &error);
1459
1465
break ;
1460
1466
}
1461
- assert (status == napi_ok );
1467
+ NAPI_FATAL_IF_FAILED (status, " Error::New " , " napi_create_error " );
1462
1468
}
1463
1469
}
1464
1470
}
@@ -1474,6 +1480,10 @@ inline Error Error::New(napi_env env, const std::string& message) {
1474
1480
return Error::New<Error>(env, message.c_str (), message.size (), napi_create_error);
1475
1481
}
1476
1482
1483
+ inline NAPI_NO_RETURN void Error::Fatal (const char * location, const char * message) {
1484
+ napi_fatal_error (location, message);
1485
+ }
1486
+
1477
1487
inline Error::Error () : ObjectReference(), _message(nullptr ) {
1478
1488
}
1479
1489
@@ -1483,7 +1493,7 @@ inline Error::Error(napi_env env, napi_value value) : ObjectReference(env, nullp
1483
1493
1484
1494
// Avoid infinite recursion in the failure case.
1485
1495
// 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 " );
1487
1497
}
1488
1498
}
1489
1499
@@ -1661,9 +1671,7 @@ inline Reference<T>::Reference(const Reference<T>& other)
1661
1671
// Copying is a limited scenario (currently only used for Error object) and always creates a
1662
1672
// strong reference to the given value even if the incoming reference is weak.
1663
1673
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" );
1667
1675
}
1668
1676
}
1669
1677
0 commit comments