php-http / curl-client

cURL client

Home Page:http://httplug.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ErrorPlugin and sendAsyncRequest() incompatibility

tuupola opened this issue · comments

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
*/