guzzle
guzzle copied to clipboard
Possible TypeError when type hinting the Pool rejected callable with a RequestException
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
Using TransferException will throw [GuzzleHttp\Exception\RequestException], how to solve it?
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.
Not stale.
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?
7.5 is affected too.
Any solution for this ?
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.
This is just a documentation issue. If you are unsure of the correct type to use, you could just put Throwable there.
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.