exception_filter_basic.phpt 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. --TEST--
  2. Test V8::setExceptionFilter() : Simple test
  3. --SKIPIF--
  4. <?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
  5. --FILE--
  6. <?php
  7. class myv8 extends V8Js
  8. {
  9. public function throwException(string $message) {
  10. throw new Exception($message);
  11. }
  12. }
  13. class ExceptionFilter {
  14. private $ex;
  15. public function __construct(Throwable $ex) {
  16. echo "ExceptionFilter::__construct called!\n";
  17. var_dump($ex->getMessage());
  18. $this->ex = $ex;
  19. }
  20. public function getMessage() {
  21. echo "getMessage called\n";
  22. return $this->ex->getMessage();
  23. }
  24. }
  25. $v8 = new myv8();
  26. $v8->setExceptionFilter(function (Throwable $ex) {
  27. echo "exception filter called.\n";
  28. return new ExceptionFilter($ex);
  29. });
  30. $v8->executeString('
  31. try {
  32. PHP.throwException("Oops");
  33. }
  34. catch (e) {
  35. var_dump(e.getMessage()); // calls ExceptionFilter::getMessage
  36. var_dump(typeof e.getTrace);
  37. }
  38. ', null, V8Js::FLAG_PROPAGATE_PHP_EXCEPTIONS);
  39. ?>
  40. ===EOF===
  41. --EXPECT--
  42. exception filter called.
  43. ExceptionFilter::__construct called!
  44. string(4) "Oops"
  45. getMessage called
  46. string(4) "Oops"
  47. string(9) "undefined"
  48. ===EOF===