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 @@ -1355,17 +1355,17 @@ inline void Buffer<T>::EnsureInfo() const {
1355
1355
inline Error Error::New (napi_env env) {
1356
1356
napi_status status;
1357
1357
napi_value error = nullptr ;
1358
- if (Napi::Env (env).IsExceptionPending ()) {
1359
- status = napi_get_and_clear_last_exception (env, &error);
1360
- assert (status == napi_ok);
1361
- }
1362
- else {
1363
- // No JS exception is pending, so check for NAPI error info.
1364
- const napi_extended_error_info* info;
1365
- status = napi_get_last_error_info (env, &info);
1366
- assert (status == napi_ok);
1367
1358
1368
- if (status == napi_ok) {
1359
+ const napi_extended_error_info* info;
1360
+ status = napi_get_last_error_info (env, &info);
1361
+ assert (status == napi_ok);
1362
+
1363
+ if (status == napi_ok) {
1364
+ if (info->error_code == napi_pending_exception) {
1365
+ status = napi_get_and_clear_last_exception (env, &error);
1366
+ assert (status == napi_ok);
1367
+ }
1368
+ else {
1369
1369
const char * error_message = info->error_message != nullptr ?
1370
1370
info->error_message : " Error in native callback" ;
1371
1371
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