Skip to content

Commit 5f7e7d3

Browse files
committed
Support throwing Throwable as-is (PHP 7+)
1 parent da3220a commit 5f7e7d3

File tree

3 files changed

+17
-28
lines changed

3 files changed

+17
-28
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,17 @@ Once the promise is fulfilled, this function will return whatever the promise
7575
resolved to.
7676

7777
Once the promise is rejected, this will throw whatever the promise rejected
78-
with. If the promise did not reject with an `Exception`, then this function
79-
will throw an `UnexpectedValueException` instead.
78+
with. If the promise did not reject with an `Exception` or `Throwable` (PHP 7+),
79+
then this function will throw an `UnexpectedValueException` instead.
8080

8181
```php
8282
try {
8383
$result = React\Async\await($promise);
8484
// promise successfully fulfilled with $result
8585
echo 'Result: ' . $result;
86-
} catch (Exception $exception) {
87-
// promise rejected with $exception
88-
echo 'ERROR: ' . $exception->getMessage();
86+
} catch (Throwable $e) {
87+
// promise rejected with $e
88+
echo 'Error: ' . $e->getMessage();
8989
}
9090
```
9191

src/functions.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,25 @@
2828
* resolved to.
2929
*
3030
* Once the promise is rejected, this will throw whatever the promise rejected
31-
* with. If the promise did not reject with an `Exception`, then this function
32-
* will throw an `UnexpectedValueException` instead.
31+
* with. If the promise did not reject with an `Exception` or `Throwable` (PHP 7+),
32+
* then this function will throw an `UnexpectedValueException` instead.
3333
*
3434
* ```php
3535
* try {
3636
* $result = React\Async\await($promise, $loop);
3737
* // promise successfully fulfilled with $result
3838
* echo 'Result: ' . $result;
39-
* } catch (Exception $exception) {
40-
* // promise rejected with $exception
41-
* echo 'ERROR: ' . $exception->getMessage();
39+
* } catch (Throwable $e) {
40+
* // promise rejected with $e
41+
* echo 'Error: ' . $e->getMessage();
4242
* }
4343
* ```
4444
*
4545
* @param PromiseInterface $promise
4646
* @return mixed returns whatever the promise resolves to
47-
* @throws \Exception when the promise is rejected
47+
* @throws \Exception when the promise is rejected with an `Exception`
48+
* @throws \Throwable when the promise is rejected with a `Throwable` (PHP 7+)
49+
* @throws \UnexpectedValueException when the promise is rejected with an unexpected value (Promise API v1 or v2 only)
4850
*/
4951
function await(PromiseInterface $promise)
5052
{
@@ -76,16 +78,11 @@ function ($error) use (&$exception, &$rejected, &$wait) {
7678
}
7779

7880
if ($rejected) {
81+
// promise is rejected with an unexpected value (Promise API v1 or v2 only)
7982
if (!$exception instanceof \Exception && !$exception instanceof \Throwable) {
8083
$exception = new \UnexpectedValueException(
8184
'Promise rejected with unexpected value of type ' . (is_object($exception) ? get_class($exception) : gettype($exception))
8285
);
83-
} elseif (!$exception instanceof \Exception) {
84-
$exception = new \UnexpectedValueException(
85-
'Promise rejected with unexpected ' . get_class($exception) . ': ' . $exception->getMessage(),
86-
$exception->getCode(),
87-
$exception
88-
);
8986
}
9087

9188
throw $exception;

tests/AwaitTest.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,12 @@ public function testAwaitOneRejectedWithNullWillWrapInUnexpectedValueException()
4444
/**
4545
* @requires PHP 7
4646
*/
47-
public function testAwaitOneRejectedWithPhp7ErrorWillWrapInUnexpectedValueExceptionWithPrevious()
47+
public function testAwaitRejectedWithPhp7ErrorWillThrowOriginalError()
4848
{
4949
$promise = Promise\reject(new \Error('Test', 42));
5050

51-
try {
52-
React\Async\await($promise);
53-
$this->fail();
54-
} catch (\UnexpectedValueException $e) {
55-
$this->assertEquals('Promise rejected with unexpected Error: Test', $e->getMessage());
56-
$this->assertEquals(42, $e->getCode());
57-
$this->assertInstanceOf('Throwable', $e->getPrevious());
58-
$this->assertEquals('Test', $e->getPrevious()->getMessage());
59-
$this->assertEquals(42, $e->getPrevious()->getCode());
60-
}
51+
$this->setExpectedException('Error', 'Test', 42);
52+
React\Async\await($promise);
6153
}
6254

6355
public function testAwaitOneResolved()

0 commit comments

Comments
 (0)