Skip to content

Commit 848d45b

Browse files
authored
Merge pull request #57 from clue-labs/throwables
Improve error reporting by appending previous message for `Throwable`s
2 parents d31a6ed + 0fd2096 commit 848d45b

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/functions.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,15 @@ function ($error) use (&$exception, &$rejected, &$wait, $loop) {
117117
}
118118

119119
if ($rejected) {
120-
if (!$exception instanceof \Exception) {
120+
if (!$exception instanceof \Exception && !$exception instanceof \Throwable) {
121121
$exception = new \UnexpectedValueException(
122-
'Promise rejected with unexpected value of type ' . (is_object($exception) ? get_class($exception) : gettype($exception)),
123-
0,
124-
$exception instanceof \Throwable ? $exception : null
122+
'Promise rejected with unexpected value of type ' . (is_object($exception) ? get_class($exception) : gettype($exception))
123+
);
124+
} elseif (!$exception instanceof \Exception) {
125+
$exception = new \UnexpectedValueException(
126+
'Promise rejected with unexpected ' . get_class($exception) . ': ' . $exception->getMessage(),
127+
$exception->getCode(),
128+
$exception
125129
);
126130
}
127131

tests/FunctionAwaitTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,17 @@ public function testAwaitOneRejectedWithNullWillWrapInUnexpectedValueException()
3737
*/
3838
public function testAwaitOneRejectedWithPhp7ErrorWillWrapInUnexpectedValueExceptionWithPrevious()
3939
{
40-
$promise = Promise\reject(new \Error('Test'));
40+
$promise = Promise\reject(new \Error('Test', 42));
4141

4242
try {
4343
Block\await($promise, $this->loop);
4444
$this->fail();
4545
} catch (\UnexpectedValueException $e) {
46-
$this->assertEquals('Promise rejected with unexpected value of type Error', $e->getMessage());
46+
$this->assertEquals('Promise rejected with unexpected Error: Test', $e->getMessage());
47+
$this->assertEquals(42, $e->getCode());
4748
$this->assertInstanceOf('Throwable', $e->getPrevious());
4849
$this->assertEquals('Test', $e->getPrevious()->getMessage());
50+
$this->assertEquals(42, $e->getPrevious()->getCode());
4951
}
5052
}
5153

0 commit comments

Comments
 (0)