guzzle icon indicating copy to clipboard operation
guzzle copied to clipboard

Possible TypeError when type hinting the Pool rejected callable with a RequestException

Open gdejong opened this issue 2 years ago • 10 comments

Guzzle version(s) affected: 7.4.2

Description
The documentation for using a GuzzleHttp\Pool suggests type hinting the rejected callable with a \GuzzleHttp\Exception\RequestException https://docs.guzzlephp.org/en/stable/quickstart.html#concurrent-requests

This can lead to a TypeError since it is possible to receive a \GuzzleHttp\Exception\ConnectException at this point as well.

How to reproduce

<?php
declare(strict_types=1);

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Pool;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;

require __DIR__ . "/vendor/autoload.php";

$client = new Client();

$requests = [
    new Request("GET", 'http://localhost:1337'),
];

$pool = new Pool($client, $requests, [
    'concurrency' => 5,
    'fulfilled' => static function (Response $response, $index) {
        echo "fulfilled!" . PHP_EOL;
    },
    'rejected' => static function (RequestException $reason, $index) {
        echo "rejected: " . $reason->getMessage() . PHP_EOL;
    },
]);

$pool->promise()->wait();

This results in the following error:

PHP Fatal error:  Uncaught TypeError: Argument 1 passed to {closure}() must be an instance of GuzzleHttp\Exception\RequestException, instance of GuzzleHttp\Exception\ConnectException given

Possible Solution

Changing RequestException to TransferException would prevent the TypeError:

    'rejected' => static function (TransferException $reason, $index) {
        echo "rejected: " . $reason->getMessage() . PHP_EOL;
    },

(since both RequestException and ConnectException extend the TransferException)

Can the example in the documentation be updated to prevent a TypeError from happening?

Additional context

image

gdejong avatar May 06 '22 11:05 gdejong

Using TransferException will throw [GuzzleHttp\Exception\RequestException], how to solve it?

guigui0326 avatar May 26 '22 08:05 guigui0326

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 2 weeks if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Sep 25 '22 01:09 stale[bot]

Not stale.

gdejong avatar Sep 28 '22 19:09 gdejong

I have this issue too using latest laravel 9, I tried the TransferException fix but for me is not working, I always got GuzzleHttp\Exception\ConnectException given. Any fix for this?

wassilis83 avatar Dec 12 '22 23:12 wassilis83

7.5 is affected too.

magnetik avatar Jan 17 '23 10:01 magnetik

Any solution for this ?

stempora avatar Feb 10 '23 09:02 stempora

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 2 weeks if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jun 10 '23 13:06 stale[bot]

This is just a documentation issue. If you are unsure of the correct type to use, you could just put Throwable there.

GrahamCampbell avatar Jun 10 '23 14:06 GrahamCampbell

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 2 weeks if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Mar 17 '24 08:03 stale[bot]