overblog / dataloader-php

DataLoaderPhp is a generic utility to be used as part of your application's data fetching layer to provide a simplified and consistent API over various remote data sources such as databases or web services via batching and caching.
MIT License
199 stars 21 forks source link

Throw/Unwrap PHP7 \Error in await() #37

Closed jpastoor closed 4 years ago

jpastoor commented 4 years ago

PHP7 introduced the concept of Errors in addition to Exceptions. (Both implement the shared \Throwable interface).

Current behaviour of await() is to check if $rejectedReason is an \Exception, but this does not include errors like TypeError. So when a TypeError is thrown in the promise chain, it is discarded in await() and this makes for some hard to debug situations.

Would you be open for a pull request which checks for Throwable $rejectReasons as well?

if ($isPromiseCompleted) {
    // rejected ?
    if ($rejectedReason instanceof \Exception || (interface_exists('\Throwable') && $rejectedReason instanceof \Throwable)) {
        if (!$unwrap) {
            return $rejectedReason;
        }
        throw $rejectedReason;
    }

    return $resolvedValue;
}