Skip to content

Commit f492e65

Browse files
authored
Merge pull request #28 from clue-labs/garbage
Clean up any garbage references when awaiting rejected promise
2 parents bc11704 + f02bfcb commit f492e65

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/functions.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,25 @@ function (mixed $throwable) use (&$rejected, &$rejectedThrowable, &$fiber): void
246246
$throwable = new \UnexpectedValueException(
247247
'Promise rejected with unexpected value of type ' . (is_object($throwable) ? get_class($throwable) : gettype($throwable))
248248
);
249+
250+
// avoid garbage references by replacing all closures in call stack.
251+
// what a lovely piece of code!
252+
$r = new \ReflectionProperty('Exception', 'trace');
253+
$trace = $r->getValue($throwable);
254+
255+
// Exception trace arguments only available when zend.exception_ignore_args is not set
256+
// @codeCoverageIgnoreStart
257+
foreach ($trace as $ti => $one) {
258+
if (isset($one['args'])) {
259+
foreach ($one['args'] as $ai => $arg) {
260+
if ($arg instanceof \Closure) {
261+
$trace[$ti]['args'][$ai] = 'Object(' . \get_class($arg) . ')';
262+
}
263+
}
264+
}
265+
}
266+
// @codeCoverageIgnoreEnd
267+
$r->setValue($throwable, $trace);
249268
}
250269

251270
if ($fiber === null) {

tests/AwaitTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,15 @@ public function testAwaitThrowsUnexpectedValueExceptionWhenPromiseIsRejectedWith
5353
$reject(null);
5454
});
5555

56-
$this->expectException(\UnexpectedValueException::class);
57-
$this->expectExceptionMessage('Promise rejected with unexpected value of type NULL');
58-
$await($promise);
56+
try {
57+
$await($promise);
58+
} catch (\UnexpectedValueException $exception) {
59+
$this->assertInstanceOf(\UnexpectedValueException::class, $exception);
60+
$this->assertEquals('Promise rejected with unexpected value of type NULL', $exception->getMessage());
61+
$this->assertEquals(0, $exception->getCode());
62+
$this->assertNull($exception->getPrevious());
63+
$this->assertNotEquals('', $exception->getTraceAsString());
64+
}
5965
}
6066

6167
/**

0 commit comments

Comments
 (0)