Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using a Http Client Fake sequence with Pool triggers a TypeError exception #48938

Closed
mattkingshott opened this issue Nov 7, 2023 · 3 comments · Fixed by #48939
Closed

Using a Http Client Fake sequence with Pool triggers a TypeError exception #48938

mattkingshott opened this issue Nov 7, 2023 · 3 comments · Fixed by #48939

Comments

@mattkingshott
Copy link
Contributor

Laravel Version

10.30.1

PHP Version

8.2.11

Database Driver & Version

No response

Description

Owing to how concurrent HTTP requests using the HTTP client are executed (using promises), an exception can be encountered due to a sometimes incorrect type hint. Specifically line 1010 within the makePromise method of Illuminate/Http/Client/PendingRequest.

In some situations, the exception that is thrown is not a TransferException, but an OutOfBoundsException. It may be preferable to remove the type hint.

Steps To Reproduce

When using the Http Client to send concurrent requests e.g.

Http::pool(fn (Pool $pool) => [
    $pool->get('http://localhost/first'),
    $pool->get('http://localhost/second'),
    $pool->get('http://localhost/third'),
]);

And writing a test that fakes a corresponding Http response sequence e.g.

use Illuminate\Support\Facades\Http;

Http::fakeSequence()
    ->push(null, 401)
    ->push(null, 403) 
    ->push(null, 405);

The following exception is thrown:

TypeError: Illuminate\Http\Client\PendingRequest::Illuminate\Http\Client\{closure}(): Argument #1 ($e) must be of type GuzzleHttp\Exception\TransferException, OutOfBoundsException given, called in /vendor/guzzlehttp/promises/src/RejectedPromise.php on line 49

The error relates to the TransferException type hint within Illuminate/Http/Client/PendingRequest on line 1010. If the type hint is removed the test runs as expected.

This error is not present when sending requests separately (not concurrently) as promises are not used.

@mattkingshott mattkingshott changed the title Using the Http Client Fake sequence with Pool triggers a TypeError exception Using a Http Client Fake sequence with Pool triggers a TypeError exception Nov 7, 2023
@driesvints
Copy link
Member

Thanks @mattkingshott. Maybe a union type is in order here?

Copy link

github-actions bot commented Nov 7, 2023

Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!

@mattkingshott
Copy link
Contributor Author

That's the simplest option, for sure. I'll PR

taylorotwell added a commit that referenced this issue Nov 7, 2023
* Update PendingRequest.php

This fixes #48938 by making the parameter a union type and adding `OutOfBoundsException`.

* Update PendingRequest.php

Fix styling.

* Update PendingRequest.php

* formatting

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants