diff --git a/src/functions.php b/src/functions.php index 0fda798..78717cf 100644 --- a/src/functions.php +++ b/src/functions.php @@ -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 ); } diff --git a/tests/FunctionAwaitTest.php b/tests/FunctionAwaitTest.php index d50736d..68fd2d2 100644 --- a/tests/FunctionAwaitTest.php +++ b/tests/FunctionAwaitTest.php @@ -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()); } }