-
-
Notifications
You must be signed in to change notification settings - Fork 146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new set_rejection_handler()
function for unhandled rejections
#249
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
tests/FunctionSetRejectionHandlerShouldBeInvokedForUnhandled.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--TEST-- | ||
The callback given to set_rejection_handler() should be invoked for unhandled rejection | ||
--INI-- | ||
# suppress legacy PHPUnit 7 warning for Xdebug 3 | ||
xdebug.default_enable= | ||
--FILE-- | ||
<?php | ||
|
||
use function React\Promise\reject; | ||
use function React\Promise\set_rejection_handler; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
set_rejection_handler(function (Throwable $e): void { | ||
echo 'Unhandled ' . get_class($e) . ': ' . $e->getMessage() . PHP_EOL; | ||
}); | ||
|
||
reject(new RuntimeException('foo')); | ||
|
||
echo 'done' . PHP_EOL; | ||
|
||
?> | ||
--EXPECT-- | ||
Unhandled RuntimeException: foo | ||
done |
37 changes: 37 additions & 0 deletions
37
tests/FunctionSetRejectionHandlerShouldInvokeLastHandlerForUnhandled.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--TEST-- | ||
The callback given to the last set_rejection_handler() should be invoked for unhandled rejection | ||
--INI-- | ||
# suppress legacy PHPUnit 7 warning for Xdebug 3 | ||
xdebug.default_enable= | ||
--FILE-- | ||
<?php | ||
|
||
use function React\Promise\reject; | ||
use function React\Promise\set_rejection_handler; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$ret = set_rejection_handler($first = function (Throwable $e): void { | ||
echo 'THIS WILL NEVER BE CALLED' . PHP_EOL; | ||
}); | ||
|
||
// previous should be null | ||
var_dump($ret === null); | ||
|
||
$ret = set_rejection_handler(function (Throwable $e): void { | ||
echo 'Unhandled ' . get_class($e) . ': ' . $e->getMessage() . PHP_EOL; | ||
}); | ||
|
||
// previous rejection handler should be first rejection handler callback | ||
var_dump($ret === $first); | ||
|
||
reject(new RuntimeException('foo')); | ||
|
||
echo 'done' . PHP_EOL; | ||
|
||
?> | ||
--EXPECT-- | ||
bool(true) | ||
bool(true) | ||
Unhandled RuntimeException: foo | ||
done |
30 changes: 30 additions & 0 deletions
30
tests/FunctionSetRejectionHandlerThatHasUnhandledShouldReportUnhandled.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--TEST-- | ||
The callback given to set_rejection_handler() should be invoked for outer unhandled rejection but should use default rejection handler for unhandled rejection | ||
--INI-- | ||
# suppress legacy PHPUnit 7 warning for Xdebug 3 | ||
xdebug.default_enable= | ||
--FILE-- | ||
<?php | ||
|
||
use function React\Promise\reject; | ||
use function React\Promise\set_rejection_handler; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
set_rejection_handler(function (Throwable $e): void { | ||
reject(new \UnexpectedValueException('bar')); | ||
|
||
echo 'Unhandled ' . get_class($e) . ': ' . $e->getMessage() . PHP_EOL; | ||
}); | ||
|
||
reject(new RuntimeException('foo')); | ||
|
||
echo 'done' . PHP_EOL; | ||
|
||
?> | ||
--EXPECTF-- | ||
Unhandled promise rejection with UnexpectedValueException: bar in %s:%d | ||
Stack trace: | ||
#0 %A{main} | ||
Unhandled RuntimeException: foo | ||
done |
26 changes: 26 additions & 0 deletions
26
tests/FunctionSetRejectionHandlerThatThrowsShouldTerminateProgramForUnhandled.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--TEST-- | ||
The callback given to set_rejection_handler() should not throw an exception or the program should terminate for unhandled rejection | ||
--INI-- | ||
# suppress legacy PHPUnit 7 warning for Xdebug 3 | ||
xdebug.default_enable= | ||
--FILE-- | ||
<?php | ||
|
||
use function React\Promise\reject; | ||
use function React\Promise\set_rejection_handler; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
set_rejection_handler(function (Throwable $e): void { | ||
throw new \UnexpectedValueException('This function should never throw'); | ||
}); | ||
|
||
reject(new RuntimeException('foo')); | ||
|
||
echo 'NEVER'; | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Uncaught UnexpectedValueException from unhandled promise rejection handler: This function should never throw in %s:%d | ||
Stack trace: | ||
#0 %A{main} |
24 changes: 24 additions & 0 deletions
24
...ctionSetRejectionHandlerThatTriggersDefaultHandlerShouldTerminateProgramForUnhandled.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--TEST-- | ||
The callback given to set_rejection_handler() may trigger a fatal error for unhandled rejection | ||
--INI-- | ||
# suppress legacy PHPUnit 7 warning for Xdebug 3 | ||
xdebug.default_enable= | ||
--FILE-- | ||
<?php | ||
|
||
use function React\Promise\reject; | ||
use function React\Promise\set_rejection_handler; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
set_rejection_handler(function (Throwable $e): void { | ||
trigger_error('Unexpected ' . get_class($e) . ': ' .$e->getMessage(), E_USER_ERROR); | ||
}); | ||
|
||
reject(new RuntimeException('foo')); | ||
|
||
echo 'NEVER'; | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Unexpected RuntimeException: foo in %s line %d |
35 changes: 35 additions & 0 deletions
35
...RejectionHandlerThatTriggersErrorHandlerThatThrowsShouldTerminateProgramForUnhandled.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--TEST-- | ||
The callback given to set_rejection_handler() may trigger a fatal error which in turn throws an exception which will terminate the program for unhandled rejection | ||
--INI-- | ||
# suppress legacy PHPUnit 7 warning for Xdebug 3 | ||
xdebug.default_enable= | ||
--FILE-- | ||
<?php | ||
|
||
use function React\Promise\reject; | ||
use function React\Promise\set_rejection_handler; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
set_error_handler(function (int $_, string $errstr): void { | ||
throw new \OverflowException('This function should never throw'); | ||
}); | ||
|
||
set_rejection_handler(function (Throwable $e): void { | ||
trigger_error($e->getMessage(), E_USER_ERROR); | ||
}); | ||
|
||
reject(new RuntimeException('foo')); | ||
|
||
echo 'NEVER'; | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Uncaught OverflowException from unhandled promise rejection handler: This function should never throw in %s:%d | ||
Stack trace: | ||
#0 [internal function]: {closure}(%S) | ||
#1 %s(%d): trigger_error(%S) | ||
#2 %s/src/Internal/RejectedPromise.php(%d): {closure}(%S) | ||
#3 %s/src/functions.php(%d): React\Promise\Internal\RejectedPromise->__destruct() | ||
#4 %s(%d): React\Promise\reject(%S) | ||
#5 %A{main} |
36 changes: 36 additions & 0 deletions
36
...ejectionHandlerThatUsesNestedSetRejectionHandlerShouldInvokeInnerHandlerForUnhandled.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--TEST-- | ||
The callback given to set_rejection_handler() should be invoked for outer unhandled rejection and may set new rejection handler for inner unhandled rejection | ||
--INI-- | ||
# suppress legacy PHPUnit 7 warning for Xdebug 3 | ||
xdebug.default_enable= | ||
--FILE-- | ||
<?php | ||
|
||
use function React\Promise\reject; | ||
use function React\Promise\set_rejection_handler; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
set_rejection_handler(function (Throwable $e): void { | ||
$ret = set_rejection_handler(function (Throwable $e): void { | ||
echo 'Unhandled inner ' . get_class($e) . ': ' . $e->getMessage() . PHP_EOL; | ||
}); | ||
|
||
// previous rejection handler should be unset while handling a rejection | ||
var_dump($ret === null); | ||
|
||
reject(new \UnexpectedValueException('bar')); | ||
|
||
echo 'Unhandled outer ' . get_class($e) . ': ' . $e->getMessage() . PHP_EOL; | ||
}); | ||
|
||
reject(new RuntimeException('foo')); | ||
|
||
echo 'done' . PHP_EOL; | ||
|
||
?> | ||
--EXPECT-- | ||
bool(true) | ||
Unhandled inner UnexpectedValueException: bar | ||
Unhandled outer RuntimeException: foo | ||
done |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello.
Why did you make this handler one-time use? In this case, it's hard to even guess on which promise it will be used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, it's one time use because this is inside the promise that is not handled when rejected. It will only ever run once. It might run again for another promise, but not this specific one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But there is no connection between the handler and the promise here. I can't explicitly specify which promise this handler is for. And if the user completes another promise without handling the exception, they will not be notified about it, while the promise for which I would like to define a handler will complete without a handler.
Are you considering the possibility of adding a function to set a global persistent handler for all promises? Or add the ability to specify a chain of promises for the handler?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @roxblnfk, took a closer look at this with @clue and it seems like following exceptions are currently not being handled correctly. We'll look into this, create some additional tests for multiple exceptions to define our expected behavior and implement a fix.
Thanks for taking the time and bringing this up 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok I think I misunderstood your initial comment. You are right, this shouldn't be a one use only handler but a persistent one instead. We're looking into adding additional tests and a fix for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks.