Skip to content
Closed
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
26 changes: 18 additions & 8 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function sleep($time, LoopInterface $loop)
* @param LoopInterface $loop
* @param null|float $timeout (optional) maximum timeout in seconds or null=wait forever
* @return mixed returns whatever the promise resolves to
* @throws Exception when the promise is rejected
* @throws \Throwable when the promise is rejected
* @throws TimeoutException if the $timeout is given and triggers
*/
function await(PromiseInterface $promise, LoopInterface $loop, $timeout = null)
Expand Down Expand Up @@ -108,7 +108,7 @@ function ($error) use (&$exception, &$rejected, &$wait, $loop) {
}
);

// Explicitly overwrite argument with null value. This ensure that this
// Explicitly overwrite argument with null value. This ensures that this
// argument does not show up in the stack trace in PHP 7+ only.
$promise = null;

Expand All @@ -117,12 +117,22 @@ function ($error) use (&$exception, &$rejected, &$wait, $loop) {
}

if ($rejected) {
if (!$exception instanceof \Exception) {
$exception = new \UnexpectedValueException(
'Promise rejected with unexpected value of type ' . (is_object($exception) ? get_class($exception) : gettype($exception)),
0,
$exception instanceof \Throwable ? $exception : null
);
if (version_compare(PHP_VERSION, '7.0', '>=')) {
if (!$exception instanceof \Throwable) {
$exception = new \UnexpectedValueException(
'Promise rejected with unexpected value of type ' . (is_object($exception) ? get_class($exception) : gettype($exception)),
0,
null
);
}
} else {
if (!$exception instanceof \Exception) {
$exception = new \UnexpectedValueException(
'Promise rejected with unexpected value of type ' . (is_object($exception) ? get_class($exception) : gettype($exception)),
0,
null
);
}
}

throw $exception;
Expand Down
19 changes: 12 additions & 7 deletions tests/FunctionAwaitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,22 @@ public function testAwaitOneRejectedWithNullWillWrapInUnexpectedValueException()
/**
* @requires PHP 7
*/
public function testAwaitOneRejectedWithPhp7ErrorWillWrapInUnexpectedValueExceptionWithPrevious()
public function testAwaitOneRejectedWithPhp7Error()
{
$promise = Promise\reject(new \Error('Test'));
$expected = new \Error('test');
$promise = $this->createPromiseRejected($expected);

// PHPUnit doesn't have something like setExpectedThrowable,
// this try catch block tests if the Error is actually thrown
// when calling Block\await
try {
Block\await($promise, $this->loop);
$this->fail();
} catch (\UnexpectedValueException $e) {
$this->assertEquals('Promise rejected with unexpected value of type Error', $e->getMessage());
$this->assertInstanceOf('Throwable', $e->getPrevious());
$this->assertEquals('Test', $e->getPrevious()->getMessage());

$this->fail('Expected \Error to be thrown, did not happen');
}

catch (\Error $error) {
self::assertSame($expected, $error);
}
}

Expand Down