Skip to content

Commit bf51bf5

Browse files
committed
document (re-)throwing behaviour from exception filter
1 parent b635a16 commit bf51bf5

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,5 +413,7 @@ via `getPrevious` method.
413413

414414
Consider that the JS code has access to methods like `getTrace` on the exception
415415
object. This might be unwanted behaviour, if you execute untrusted code.
416-
Using `setExceptionFilter` method a callable can be provided, that converts
417-
the PHP exception to not expose unwanted information.
416+
Using `setExceptionFilter` method a callable can be provided, that may convert
417+
the PHP exception to some other value that is safe to expose. The filter may
418+
also decide not to propagate the exception to JS at all by either re-throwing
419+
the passed exception or throwing another exception.

tests/exception_filter_004.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
Test V8::setExceptionFilter() : Filter handling on exception in factory
2+
Test V8::setExceptionFilter() : Filter handling on exception in converter
33
--SKIPIF--
44
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
55
--FILE--

tests/exception_filter_006.phpt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
--TEST--
2+
Test V8::setExceptionFilter() : re-throw exception in exception filter
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+
14+
$v8 = new myv8();
15+
$v8->setExceptionFilter(function (Throwable $ex) {
16+
// re-throw exception so it is not forwarded
17+
throw $ex;
18+
});
19+
20+
try {
21+
$v8->executeString('
22+
try {
23+
PHP.throwException("Oops");
24+
print("done\\n");
25+
}
26+
catch (e) {
27+
print("caught\\n");
28+
var_dump(e);
29+
}
30+
', null, V8Js::FLAG_PROPAGATE_PHP_EXCEPTIONS);
31+
} catch (Exception $ex) {
32+
echo "caught in php: " . $ex->getMessage() . PHP_EOL;
33+
}
34+
?>
35+
===EOF===
36+
--EXPECT--
37+
caught in php: Oops
38+
===EOF===

0 commit comments

Comments
 (0)