[HttpClient] make toStream() throw by default

This commit is contained in:
Nicolas Grekas 2019-07-16 08:40:46 +02:00
parent 52e9fb91ff
commit 90e46ab13b
2 changed files with 12 additions and 3 deletions

View File

@ -92,7 +92,7 @@ final class Psr18Client implements ClientInterface, RequestFactoryInterface, Str
}
}
$body = isset(class_uses($response)[ResponseTrait::class]) ? $response->toStream() : StreamWrapper::createResource($response, $this->client);
$body = isset(class_uses($response)[ResponseTrait::class]) ? $response->toStream(false) : StreamWrapper::createResource($response, $this->client);
return $psrResponse->withBody($this->streamFactory->createStreamFromResource($body));
} catch (TransportExceptionInterface $e) {

View File

@ -21,6 +21,10 @@ use Symfony\Component\HttpClient\Exception\RedirectionException;
use Symfony\Component\HttpClient\Exception\ServerException;
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Component\HttpClient\Internal\ClientState;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
/**
* Implements the common logic for response classes.
@ -182,11 +186,16 @@ trait ResponseTrait
* Casts the response to a PHP stream resource.
*
* @return resource|null
*
* @throws TransportExceptionInterface When a network error occurs
* @throws RedirectionExceptionInterface On a 3xx when $throw is true and the "max_redirects" option has been reached
* @throws ClientExceptionInterface On a 4xx when $throw is true
* @throws ServerExceptionInterface On a 5xx when $throw is true
*/
public function toStream()
public function toStream(bool $throw = true)
{
// Ensure headers arrived
$this->getStatusCode();
$this->getHeaders($throw);
return StreamWrapper::createResource($this, null, $this->content, $this->handle && 'stream' === get_resource_type($this->handle) ? $this->handle : null);
}