Skip to content

Commit

Permalink
Throw/Unwrap PHP7 \Error in await()
Browse files Browse the repository at this point in the history
  • Loading branch information
jpastoor committed May 25, 2020
1 parent 9828eb5 commit 9356d81
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/DataLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ function ($reason) use (&$isPromiseCompleted, &$rejectedReason) {
//Promise is completed?
if ($isPromiseCompleted) {
// rejected ?
if ($rejectedReason instanceof \Exception) {
if ($rejectedReason instanceof \Exception || (interface_exists('\Throwable') && $rejectedReason instanceof \Throwable)) {
if (!$unwrap) {
return $rejectedReason;
}
Expand Down
23 changes: 22 additions & 1 deletion tests/DataLoadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -846,13 +846,34 @@ public function testAwaitShouldThrowTheRejectReasonOfRejectedPromiseWithoutNeedi
DataLoader::await(self::$promiseAdapter->createRejected(new \Exception('Rejected!')));
}

/**
* @runInSeparateProcess
*
* We cannot use the standard expectedException annotations here since the class does not exist in PHP5.
*/
public function testAwaitShouldThrowThrowable()
{
if (PHP_MAJOR_VERSION >= 7) {
try {
DataLoader::await(self::$promiseAdapter->createRejected(new \Error('Rejected Error!')));

// If we got here, it means no Error is thrown, so fail the test
throw new \Exception("Expected \Error to be thrown.");
} catch (\Error $error) {
if ($error->getMessage() != 'Rejected Error!') {
throw new \Exception("Expected Rejected Error! as message, but got: " . $error->getMessage());
}
}
}
}

public function cacheKey($key)
{
$cacheKey = [];
$key = (array)$key;
ksort($key);
foreach ($key as $k => $value) {
$cacheKey[] = $k.':'.$value;
$cacheKey[] = $k . ':' . $value;
}

return implode(',', $cacheKey);
Expand Down

0 comments on commit 9356d81

Please sign in to comment.