guzzle / guzzle

Guzzle, an extensible PHP HTTP client

Home Page:https://docs.guzzlephp.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

No exception gets thrown on responses with status code >= 400

davisenra opened this issue · comments

commented

Guzzle version(s) affected: 7.5.3
PHP version: PHP 8.0.30
cURL version: 7.74.0

Description
After adding a CurlHandler + handler stack to my Guzzlehttp\Client instance it no longers throws exceptions for error responses.

How to reproduce
The client I'm using:

$stack = new HandlerStack();
$stack->setHandler(new CurlHandler());

$stack->push(Middleware::mapRequest(function(RequestInterface $request) {
    // custom internal logging logic here

    return $request;
}));

$stack->push(Middleware::mapResponse(function(ResponseInterface $response) {
    // custom internal logging logic here

    $response->getBody()->rewind();

    return $response;
}));

$this->httpClient = new Client(['handler' => $stack]);

The request:

$response = $this->httpClient->request('GET', $endpoint, [
    'query' => [
        'page' => $page,
        'limit' => $limit,
    ],
    'headers' => [
        'Authorization' => "Bearer $token"
    ],
]);

No exception get's thrown when $response->getStatusCode() >= 400..

This is happening because you're using a custom stack and not pushing our https errors middleware. Usually, it would get added by the stack create method: https://github.com/guzzle/guzzle/blob/7.8/src/HandlerStack.php#L50. ;)