@@ -443,99 +443,6 @@ namespace Napi {
443
443
void EnsureInfo () const ;
444
444
};
445
445
446
- /*
447
- * The NAPI Error class wraps a JavaScript Error object in a way that enables it
448
- * to traverse a C++ stack and be thrown and caught as a C++ exception.
449
- *
450
- * If a NAPI API call fails without executing any JavaScript code (for example due
451
- * to an invalid argument), then the NAPI wrapper automatically converts and throws
452
- * the error as a C++ exception of type Napi::Error.
453
- *
454
- * If a JavaScript function called by C++ code via NAPI throws a JavaScript exception,
455
- * then the NAPI wrapper automatically converts and throws it as a C++ exception of type
456
- * Napi::Error.
457
- *
458
- * If a C++ exception of type Napi::Error escapes from a NAPI C++ callback, then
459
- * the NAPI wrapper automatically converts and throws it as a JavaScript exception.
460
- *
461
- * Catching a C++ exception of type Napi::Error also clears the JavaScript exception.
462
- * Of course it may be then re-thrown, which restores the JavaScript exception.
463
- *
464
- * Example 1 - Throwing an exception:
465
- *
466
- * Napi::Env env = ...
467
- * throw env.NewError("Example exception");
468
- * // Following C++ statements will not be executed.
469
- * // The exception will bubble up as a C++ exception of type Napi::Error,
470
- * // until it is either caught while still in C++, or else automatically
471
- * // re-thrown as a JavaScript exception when the callback returns to JavaScript.
472
- *
473
- * Example 2 - Ignoring a NAPI exception:
474
- *
475
- * Napi::Function jsFunctionThatThrows = someObj.AsFunction();
476
- * jsFunctionThatThrows({ arg1, arg2 });
477
- * // Following C++ statements will not be executed.
478
- * // The exception will bubble up as a C++ exception of type Napi::Error,
479
- * // until it is either caught while still in C++, or else automatically
480
- * // re-thrown as a JavaScript exception when the callback returns to JavaScript.
481
- *
482
- * Example 3 - Handling a NAPI exception:
483
- *
484
- * Napi::Function jsFunctionThatThrows = someObj.AsFunction();
485
- * try {
486
- * jsFunctionThatThrows({ arg1, arg2 });
487
- * }
488
- * catch (const Napi::Error& e) {
489
- * cerr << "Caught JavaScript exception: " + e.what();
490
- * // Since the exception was caught here, it will not be re-thrown as
491
- * // a JavaScript exception.
492
- * }
493
- */
494
- class Error : public Object , public std ::exception {
495
- public:
496
- static Error New (napi_env env);
497
- static Error New (napi_env env, const char * message);
498
- static Error New (napi_env env, const std::string& message);
499
-
500
- Error ();
501
- Error (napi_env env, napi_value value);
502
-
503
- const std::string& Message () const NAPI_NOEXCEPT;
504
- void ThrowAsJavaScriptException () const ;
505
-
506
- const char * what () const NAPI_NOEXCEPT override ;
507
-
508
- protected:
509
- typedef napi_status (*create_error_fn)(napi_env envb, napi_value msg, napi_value* result);
510
-
511
- template <typename TError>
512
- static TError New (napi_env env,
513
- const char * message,
514
- size_t length,
515
- create_error_fn create_error);
516
-
517
- private:
518
- mutable std::string _message;
519
- };
520
-
521
- class TypeError : public Error {
522
- public:
523
- static TypeError New (napi_env env, const char * message);
524
- static TypeError New (napi_env env, const std::string& message);
525
-
526
- TypeError ();
527
- TypeError (napi_env env, napi_value value);
528
- };
529
-
530
- class RangeError : public Error {
531
- public:
532
- static RangeError New (napi_env env, const char * message);
533
- static RangeError New (napi_env env, const std::string& message);
534
-
535
- RangeError ();
536
- RangeError (napi_env env, napi_value value);
537
- };
538
-
539
446
/*
540
447
* Holds a counted reference to a value; initially a weak reference unless otherwise specified.
541
448
* May be changed to/from a strong reference by adjusting the refcount. The referenced value
@@ -660,6 +567,104 @@ namespace Napi {
660
567
ObjectReference Persistent (Object value);
661
568
FunctionReference Persistent (Function value);
662
569
570
+ /*
571
+ * The NAPI Error class wraps a JavaScript Error object in a way that enables it
572
+ * to traverse a C++ stack and be thrown and caught as a C++ exception.
573
+ *
574
+ * If a NAPI API call fails without executing any JavaScript code (for example due
575
+ * to an invalid argument), then the NAPI wrapper automatically converts and throws
576
+ * the error as a C++ exception of type Napi::Error.
577
+ *
578
+ * If a JavaScript function called by C++ code via NAPI throws a JavaScript exception,
579
+ * then the NAPI wrapper automatically converts and throws it as a C++ exception of type
580
+ * Napi::Error.
581
+ *
582
+ * If a C++ exception of type Napi::Error escapes from a NAPI C++ callback, then
583
+ * the NAPI wrapper automatically converts and throws it as a JavaScript exception.
584
+ *
585
+ * Catching a C++ exception of type Napi::Error also clears the JavaScript exception.
586
+ * Of course it may be then re-thrown, which restores the JavaScript exception.
587
+ *
588
+ * Example 1 - Throwing a N-API exception:
589
+ *
590
+ * Napi::Env env = ...
591
+ * throw Napi::Error::New(env, "Example exception");
592
+ * // Following C++ statements will not be executed.
593
+ * // The exception will bubble up as a C++ exception of type Napi::Error,
594
+ * // until it is either caught while still in C++, or else automatically
595
+ * // re-thrown as a JavaScript exception when the callback returns to JavaScript.
596
+ *
597
+ * Example 2 - Ignoring a N-API exception:
598
+ *
599
+ * Napi::Function jsFunctionThatThrows = someObj.As<Napi::Function>();
600
+ * jsFunctionThatThrows({ arg1, arg2 });
601
+ * // Following C++ statements will not be executed.
602
+ * // The exception will bubble up as a C++ exception of type Napi::Error,
603
+ * // until it is either caught while still in C++, or else automatically
604
+ * // re-thrown as a JavaScript exception when the callback returns to JavaScript.
605
+ *
606
+ * Example 3 - Handling a N-API exception:
607
+ *
608
+ * Napi::Function jsFunctionThatThrows = someObj.As<Napi::Function>();
609
+ * try {
610
+ * jsFunctionThatThrows({ arg1, arg2 });
611
+ * } catch (const Napi::Error& e) {
612
+ * cerr << "Caught JavaScript exception: " + e.Message();
613
+ * // Since the exception was caught here, it will not be re-thrown as
614
+ * // a JavaScript exception.
615
+ * }
616
+ */
617
+ class Error : public ObjectReference , public std ::exception {
618
+ public:
619
+ static Error New (napi_env env);
620
+ static Error New (napi_env env, const char * message);
621
+ static Error New (napi_env env, const std::string& message);
622
+
623
+ Error ();
624
+ Error (napi_env env, napi_value value);
625
+
626
+ // An error can be moved or copied.
627
+ Error (Error&& other);
628
+ Error& operator =(Error&& other);
629
+ Error (const Error&);
630
+ Error& operator =(Error&);
631
+
632
+ const std::string& Message () const NAPI_NOEXCEPT;
633
+ void ThrowAsJavaScriptException () const ;
634
+
635
+ const char * what () const NAPI_NOEXCEPT override ;
636
+
637
+ protected:
638
+ typedef napi_status (*create_error_fn)(napi_env envb, napi_value msg, napi_value* result);
639
+
640
+ template <typename TError>
641
+ static TError New (napi_env env,
642
+ const char * message,
643
+ size_t length,
644
+ create_error_fn create_error);
645
+
646
+ private:
647
+ mutable std::string _message;
648
+ };
649
+
650
+ class TypeError : public Error {
651
+ public:
652
+ static TypeError New (napi_env env, const char * message);
653
+ static TypeError New (napi_env env, const std::string& message);
654
+
655
+ TypeError ();
656
+ TypeError (napi_env env, napi_value value);
657
+ };
658
+
659
+ class RangeError : public Error {
660
+ public:
661
+ static RangeError New (napi_env env, const char * message);
662
+ static RangeError New (napi_env env, const std::string& message);
663
+
664
+ RangeError ();
665
+ RangeError (napi_env env, napi_value value);
666
+ };
667
+
663
668
class CallbackInfo {
664
669
public:
665
670
CallbackInfo (napi_env env, napi_callback_info info);
@@ -985,7 +990,7 @@ namespace Napi {
985
990
986
991
napi_env _env;
987
992
napi_async_work _work;
988
- Reference< Error> _error;
993
+ Error _error;
989
994
};
990
995
991
996
} // namespace Napi
0 commit comments