Skip to content

Commit d2a6608

Browse files
committed
Support throwing Throwable as-is (PHP 7+)
1 parent 7763182 commit d2a6608

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
@@ -27,23 +27,25 @@
2727
* resolved to.
2828
*
2929
* Once the promise is rejected, this will throw whatever the promise rejected
30-
* with. If the promise did not reject with an `Exception`, then this function
31-
* will throw an `UnexpectedValueException` instead.
30+
* with. If the promise did not reject with an `Exception` or `Throwable` (PHP 7+),
31+
* then this function will throw an `UnexpectedValueException` instead.
3232
*
3333
* ```php
3434
* try {
3535
* $result = React\Async\await($promise, $loop);
3636
* // promise successfully fulfilled with $result
3737
* echo 'Result: ' . $result;
38-
* } catch (Exception $exception) {
39-
* // promise rejected with $exception
40-
* echo 'ERROR: ' . $exception->getMessage();
38+
* } catch (Throwable $e) {
39+
* // promise rejected with $e
40+
* echo 'Error: ' . $e->getMessage();
4141
* }
4242
* ```
4343
*
4444
* @param PromiseInterface $promise
4545
* @return mixed returns whatever the promise resolves to
46-
* @throws \Exception when the promise is rejected
46+
* @throws \Exception when the promise is rejected with an `Exception`
47+
* @throws \Throwable when the promise is rejected with a `Throwable` (PHP 7+)
48+
* @throws \UnexpectedValueException when the promise is rejected with an unexpected value (Promise API v1 or v2 only)
4749
*/
4850
function await(PromiseInterface $promise)
4951
{
@@ -75,16 +77,11 @@ function ($error) use (&$exception, &$rejected, &$wait) {
7577
}
7678

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

9087
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)