Skip to content

Commit

Permalink
Forward compatibility with upcoming Promise v3
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Sep 3, 2022
1 parent 092fbfe commit 65748cb
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
"php": ">=7.1",
"nikic/fast-route": "^1.3",
"react/async": "^4 || ^3",
"react/http": "^1.7",
"react/promise": "^2.7"
"react/http": "^1.8@dev || ^1.7",
"react/promise": "^3@dev || ^2.7"
},
"require-dev": {
"phpunit/phpunit": "^9.5 || ^7.5",
Expand Down
5 changes: 4 additions & 1 deletion src/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,13 @@ public function __invoke(ServerRequestInterface $request, callable $next)
return $this->errorInvalidResponse($response);
}
}, function ($e) {
// Promise rejected, always a `\Throwable` as of Promise v3
assert($e instanceof \Throwable || !\method_exists(PromiseInterface::class, 'catch'));

if ($e instanceof \Throwable) {
return $this->errorInvalidException($e);
} else {
return $this->errorInvalidResponse(\React\Promise\reject($e));
return $this->errorInvalidResponse(\React\Promise\reject($e)); // @codeCoverageIgnore
}
});
} elseif ($response instanceof \Generator) {
Expand Down
4 changes: 4 additions & 0 deletions tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1534,6 +1534,10 @@ public function testHandleRequestWithMatchingRouteReturnsPromiseWhichFulfillsWit

public function testHandleRequestWithMatchingRouteReturnsPromiseWhichFulfillsWithInternalServerErrorResponseWhenHandlerReturnsPromiseWhichRejectsWithNull()
{
if (method_exists(PromiseInterface::class, 'catch')) {
$this->markTestSkipped('Only supported for legacy Promise v2, Promise v3 always rejects with Throwable');
}

$app = $this->createAppWithoutLogger();

$app->get('/users', function () {
Expand Down
4 changes: 4 additions & 0 deletions tests/ErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,10 @@ public function testInvokeWithHandlerReturningPromiseResolvingWithNullReturnsPro

public function testInvokeWithHandlerReturningPromiseRejectingWithNullReturnsPromiseResolvingWithError500Response()
{
if (method_exists(PromiseInterface::class, 'catch')) {
$this->markTestSkipped('Only supported for legacy Promise v2, Promise v3 always rejects with Throwable');
}

$handler = new ErrorHandler();

$request = new ServerRequest('GET', 'http://example.com/');
Expand Down

0 comments on commit 65748cb

Please sign in to comment.