From bf51bf52a90d2cfbbb957d9d9e2719e344df547e Mon Sep 17 00:00:00 2001 From: Stefan Siegl Date: Fri, 24 Jun 2022 13:15:40 +0200 Subject: [PATCH] document (re-)throwing behaviour from exception filter --- README.md | 6 ++++-- tests/exception_filter_004.phpt | 2 +- tests/exception_filter_006.phpt | 38 +++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 tests/exception_filter_006.phpt diff --git a/README.md b/README.md index eab1a7d..0155f2c 100644 --- a/README.md +++ b/README.md @@ -413,5 +413,7 @@ via `getPrevious` method. Consider that the JS code has access to methods like `getTrace` on the exception object. This might be unwanted behaviour, if you execute untrusted code. -Using `setExceptionFilter` method a callable can be provided, that converts -the PHP exception to not expose unwanted information. +Using `setExceptionFilter` method a callable can be provided, that may convert +the PHP exception to some other value that is safe to expose. The filter may +also decide not to propagate the exception to JS at all by either re-throwing +the passed exception or throwing another exception. diff --git a/tests/exception_filter_004.phpt b/tests/exception_filter_004.phpt index a5dec89..b35e3fc 100644 --- a/tests/exception_filter_004.phpt +++ b/tests/exception_filter_004.phpt @@ -1,5 +1,5 @@ --TEST-- -Test V8::setExceptionFilter() : Filter handling on exception in factory +Test V8::setExceptionFilter() : Filter handling on exception in converter --SKIPIF-- --FILE-- diff --git a/tests/exception_filter_006.phpt b/tests/exception_filter_006.phpt new file mode 100644 index 0000000..bf62fce --- /dev/null +++ b/tests/exception_filter_006.phpt @@ -0,0 +1,38 @@ +--TEST-- +Test V8::setExceptionFilter() : re-throw exception in exception filter +--SKIPIF-- + +--FILE-- +setExceptionFilter(function (Throwable $ex) { + // re-throw exception so it is not forwarded + throw $ex; +}); + +try { + $v8->executeString(' + try { + PHP.throwException("Oops"); + print("done\\n"); + } + catch (e) { + print("caught\\n"); + var_dump(e); + } + ', null, V8Js::FLAG_PROPAGATE_PHP_EXCEPTIONS); +} catch (Exception $ex) { + echo "caught in php: " . $ex->getMessage() . PHP_EOL; +} +?> +===EOF=== +--EXPECT-- +caught in php: Oops +===EOF===