Skip to content

Commit 85f7487

Browse files
authored
Enhance error handling in PendingRequest to convert TooManyRedirectsE… (#55998)
* Enhance error handling in PendingRequest to convert TooManyRedirectsException to ConnectionException. Add tests to verify the conversion of redirect exceptions. * fix code style * Refactor error handling in PendingRequest to simplify exception throwing logic by using null coalescing operator.
1 parent 8840840 commit 85f7487

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/Illuminate/Http/Client/PendingRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1613,7 +1613,7 @@ protected function marshalRequestExceptionWithResponse(RequestException $e)
16131613
$response = $this->populateResponse($this->newResponse($e->getResponse()))
16141614
);
16151615

1616-
throw $response->toException();
1616+
throw $response->toException() ?? new ConnectionException($e->getMessage(), 0, $e);
16171617
}
16181618

16191619
/**

tests/Http/HttpClientTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Exception;
66
use GuzzleHttp\Exception\RequestException as GuzzleRequestException;
7+
use GuzzleHttp\Exception\TooManyRedirectsException;
78
use GuzzleHttp\Middleware;
89
use GuzzleHttp\Promise\PromiseInterface;
910
use GuzzleHttp\Promise\RejectedPromise;
@@ -2609,6 +2610,38 @@ public function testSslCertificateErrorsConvertedToConnectionException()
26092610
$this->factory->head('https://ssl-error.laravel.example');
26102611
}
26112612

2613+
public function testTooManyRedirectsExceptionConvertedToConnectionException()
2614+
{
2615+
$this->factory->fake(function () {
2616+
$request = new GuzzleRequest('GET', 'https://redirect.laravel.example');
2617+
$response = new Psr7Response(301, ['Location' => 'https://redirect2.laravel.example']);
2618+
2619+
throw new TooManyRedirectsException(
2620+
'Maximum number of redirects (5) exceeded',
2621+
$request,
2622+
$response
2623+
);
2624+
});
2625+
2626+
$this->expectException(ConnectionException::class);
2627+
$this->expectExceptionMessage('Maximum number of redirects (5) exceeded');
2628+
2629+
$this->factory->maxRedirects(5)->get('https://redirect.laravel.example');
2630+
}
2631+
2632+
public function testTooManyRedirectsWithFakedRedirectChain()
2633+
{
2634+
$this->factory->fake([
2635+
'1.example.com' => $this->factory->response(null, 301, ['Location' => 'https://2.example.com']),
2636+
'2.example.com' => $this->factory->response(null, 301, ['Location' => 'https://3.example.com']),
2637+
'3.example.com' => $this->factory->response('', 200),
2638+
]);
2639+
2640+
$this->expectException(ConnectionException::class);
2641+
2642+
$this->factory->maxRedirects(1)->get('https://1.example.com');
2643+
}
2644+
26122645
public function testRequestExceptionIsNotThrownIfThePendingRequestIsSetToThrowOnFailureButTheResponseIsSuccessful()
26132646
{
26142647
$this->factory->fake([

0 commit comments

Comments
 (0)