Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,15 @@ function ($error) use (&$exception, &$rejected, &$wait, $loop) {
}

if ($rejected) {
if (!$exception instanceof \Exception) {
if (!$exception instanceof \Exception && !$exception instanceof \Throwable) {
$exception = new \UnexpectedValueException(
'Promise rejected with unexpected value of type ' . (is_object($exception) ? get_class($exception) : gettype($exception)),
0,
$exception instanceof \Throwable ? $exception : null
'Promise rejected with unexpected value of type ' . (is_object($exception) ? get_class($exception) : gettype($exception))
);
} elseif (!$exception instanceof \Exception) {
$exception = new \UnexpectedValueException(
'Promise rejected with unexpected ' . get_class($exception) . ': ' . $exception->getMessage(),
$exception->getCode(),
$exception
);
}

Expand Down
6 changes: 4 additions & 2 deletions tests/FunctionAwaitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,17 @@ public function testAwaitOneRejectedWithNullWillWrapInUnexpectedValueException()
*/
public function testAwaitOneRejectedWithPhp7ErrorWillWrapInUnexpectedValueExceptionWithPrevious()
{
$promise = Promise\reject(new \Error('Test'));
$promise = Promise\reject(new \Error('Test', 42));

try {
Block\await($promise, $this->loop);
$this->fail();
} catch (\UnexpectedValueException $e) {
$this->assertEquals('Promise rejected with unexpected value of type Error', $e->getMessage());
$this->assertEquals('Promise rejected with unexpected Error: Test', $e->getMessage());
$this->assertEquals(42, $e->getCode());
$this->assertInstanceOf('Throwable', $e->getPrevious());
$this->assertEquals('Test', $e->getPrevious()->getMessage());
$this->assertEquals(42, $e->getPrevious()->getCode());
}
}

Expand Down