File tree Expand file tree Collapse file tree 4 files changed +24
-14
lines changed Expand file tree Collapse file tree 4 files changed +24
-14
lines changed Original file line number Diff line number Diff line change @@ -1373,17 +1373,17 @@ inline void Buffer<T>::EnsureInfo() const {
1373
1373
inline Error Error::New (napi_env env) {
1374
1374
napi_status status;
1375
1375
napi_value error = nullptr ;
1376
- if (Napi::Env (env).IsExceptionPending ()) {
1377
- status = napi_get_and_clear_last_exception (env, &error);
1378
- assert (status == napi_ok);
1379
- }
1380
- else {
1381
- // No JS exception is pending, so check for NAPI error info.
1382
- const napi_extended_error_info* info;
1383
- status = napi_get_last_error_info (env, &info);
1384
- assert (status == napi_ok);
1385
1376
1386
- if (status == napi_ok) {
1377
+ const napi_extended_error_info* info;
1378
+ status = napi_get_last_error_info (env, &info);
1379
+ assert (status == napi_ok);
1380
+
1381
+ if (status == napi_ok) {
1382
+ if (info->error_code == napi_pending_exception) {
1383
+ status = napi_get_and_clear_last_exception (env, &error);
1384
+ assert (status == napi_ok);
1385
+ }
1386
+ else {
1387
1387
const char * error_message = info->error_message != nullptr ?
1388
1388
info->error_message : " Error in native callback" ;
1389
1389
napi_value message;
Original file line number Diff line number Diff line change @@ -755,7 +755,7 @@ napi_status napi_get_last_error_info(napi_env env,
755
755
error_messages[env->last_error .error_code ];
756
756
757
757
*result = &(env->last_error );
758
- return napi_clear_last_error (env) ;
758
+ return napi_ok ;
759
759
}
760
760
761
761
napi_status napi_create_function (napi_env env,
Original file line number Diff line number Diff line change @@ -4,7 +4,12 @@ using namespace Napi;
4
4
5
5
namespace {
6
6
7
- void ThrowError (const CallbackInfo& info) {
7
+ void ThrowApiError (const CallbackInfo& info) {
8
+ // Attempting to call an empty function value will throw an API error.
9
+ Function (info.Env (), nullptr ).Call ({});
10
+ }
11
+
12
+ void ThrowJSError (const CallbackInfo& info) {
8
13
std::string message = info[0 ].As <String>().Utf8Value ();
9
14
throw Error::New (info.Env (), message);
10
15
}
@@ -76,7 +81,8 @@ void CatchAndRethrowErrorThatEscapesScope(const CallbackInfo& info) {
76
81
77
82
Object InitError (Env env) {
78
83
Object exports = Object::New (env);
79
- exports[" throwError" ] = Function::New (env, ThrowError);
84
+ exports[" throwApiError" ] = Function::New (env, ThrowApiError);
85
+ exports[" throwJSError" ] = Function::New (env, ThrowJSError);
80
86
exports[" throwTypeError" ] = Function::New (env, ThrowTypeError);
81
87
exports[" throwRangeError" ] = Function::New (env, ThrowRangeError);
82
88
exports[" catchError" ] = Function::New (env, CatchError);
Original file line number Diff line number Diff line change @@ -3,7 +3,11 @@ const buildType = process.config.target_defaults.default_configuration;
3
3
const binding = require ( `./build/${ buildType } /binding.node` ) ;
4
4
const assert = require ( 'assert' ) ;
5
5
6
- assert . throws ( ( ) => binding . error . throwError ( 'test' ) , err => {
6
+ assert . throws ( ( ) => binding . error . throwApiError ( 'test' ) , err => {
7
+ return err instanceof Error && err . message . includes ( 'Invalid' ) ;
8
+ } ) ;
9
+
10
+ assert . throws ( ( ) => binding . error . throwJSError ( 'test' ) , err => {
7
11
return err instanceof Error && err . message === 'test' ;
8
12
} ) ;
9
13
You can’t perform that action at this time.
0 commit comments