php-http / curl-client

cURL client
http://httplug.io
MIT License
443 stars 28 forks source link

ErrorPlugin and sendAsyncRequest() incompatibility #27

Closed tuupola closed 7 years ago

tuupola commented 7 years ago

When using and async requests CurlPromise seems to ignore the ErrorPlugin and throws the original exception instead. For example in case of 404 response ErrorPlugin is supposed to thrown ClientErrorException.

use Http\Client\Curl\Client as Curl;
use Http\Message\MessageFactory\DiactorosMessageFactory;
use Http\Message\StreamFactory\DiactorosStreamFactory;
use Http\Client\Common\Plugin\ErrorPlugin;
use Http\Client\Common\PluginClient;
use Psr\Http\Message\ResponseInterface;

$nosuch = (new DiactorosMessageFactory)->createRequest("GET", "http://google.com/nosuch");
$curl = new Curl(new DiactorosMessageFactory(), new DiactorosStreamFactory());
$client = new PluginClient($curl, [new ErrorPlugin]);

$promise = $client
    ->sendAsyncRequest($nosuch)
    ->then(function (ResponseInterface $response) {
        print date("H:s") . " got foo\n";
        return $response;
    });

try {
    $promise->wait();
} catch (\Exception $exception) {
    print "async: " . get_class($exception) . "\n";
}

try {
    $client->sendRequest($nosuch);
} catch (\Exception $exception) {
    print "sync: " . get_class($exception) . "\n";
}

/* 
async: Http\Client\Exception\RequestException
sync: Http\Client\Common\Exception\ClientErrorException
*/