Skip to content

Commit 21f12d8

Browse files
authored
[12.x] Fix Possible Undefined Variables (#56292)
* declare the $request variable outside the nullsafe chain * declare the $response variable outside the nullsafe chain * add tests for ConnectionException converter catch block * tests clean-up: remove redundant vars
1 parent 03469f2 commit 21f12d8

File tree

2 files changed

+81
-4
lines changed

2 files changed

+81
-4
lines changed

src/Illuminate/Http/Client/PendingRequest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,8 +1572,10 @@ protected function marshalConnectionException(ConnectException $e)
15721572
{
15731573
$exception = new ConnectionException($e->getMessage(), 0, $e);
15741574

1575+
$request = new Request($e->getRequest());
1576+
15751577
$this->factory?->recordRequestResponsePair(
1576-
$request = new Request($e->getRequest()), null
1578+
$request, null
15771579
);
15781580

15791581
$this->dispatchConnectionFailedEvent($request, $exception);
@@ -1591,8 +1593,10 @@ protected function marshalRequestExceptionWithoutResponse(RequestException $e)
15911593
{
15921594
$exception = new ConnectionException($e->getMessage(), 0, $e);
15931595

1596+
$request = new Request($e->getRequest());
1597+
15941598
$this->factory?->recordRequestResponsePair(
1595-
$request = new Request($e->getRequest()), null
1599+
$request, null
15961600
);
15971601

15981602
$this->dispatchConnectionFailedEvent($request, $exception);
@@ -1608,9 +1612,11 @@ protected function marshalRequestExceptionWithoutResponse(RequestException $e)
16081612
*/
16091613
protected function marshalRequestExceptionWithResponse(RequestException $e)
16101614
{
1615+
$response = $this->populateResponse($this->newResponse($e->getResponse()));
1616+
16111617
$this->factory?->recordRequestResponsePair(
16121618
new Request($e->getRequest()),
1613-
$response = $this->populateResponse($this->newResponse($e->getResponse()))
1619+
$response
16141620
);
16151621

16161622
throw $response->toException() ?? new ConnectionException($e->getMessage(), 0, $e);

tests/Http/HttpClientTest.php

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Tests\Http;
44

55
use Exception;
6+
use GuzzleHttp\Exception\ConnectException;
67
use GuzzleHttp\Exception\RequestException as GuzzleRequestException;
78
use GuzzleHttp\Exception\TooManyRedirectsException;
89
use GuzzleHttp\Middleware;
@@ -2379,7 +2380,7 @@ public function testRequestCanBeModifiedInRetryCallbackInPool()
23792380

23802381
public function testHandleRequestExeptionWithNoResponseInPoolConsideredConnectionException()
23812382
{
2382-
$requestException = new \GuzzleHttp\Exception\RequestException('Error', new \GuzzleHttp\Psr7\Request('GET', '/'));
2383+
$requestException = new GuzzleRequestException('Error', new \GuzzleHttp\Psr7\Request('GET', '/'));
23832384
$this->factory->fake([
23842385
'noresponse.com' => new RejectedPromise($requestException),
23852386
]);
@@ -2610,6 +2611,76 @@ public function testSslCertificateErrorsConvertedToConnectionException()
26102611
$this->factory->head('https://ssl-error.laravel.example');
26112612
}
26122613

2614+
public function testConnectExceptionIsConvertedToConnectionExceptionEvenWhenWithoutFactory()
2615+
{
2616+
$this->expectException(ConnectionException::class);
2617+
$this->expectExceptionMessage('cURL error 60: SSL certificate problem');
2618+
2619+
$pendingRequest = new PendingRequest();
2620+
2621+
$pendingRequest->setHandler(function () {
2622+
throw new ConnectException(
2623+
'cURL error 60: SSL certificate problem: unable to get local issuer certificate',
2624+
new GuzzleRequest('HEAD', 'https://ssl-error.laravel.example')
2625+
);
2626+
});
2627+
2628+
$pendingRequest->head('https://ssl-error.laravel.example');
2629+
}
2630+
2631+
public function testRequestExceptionWithoutResponseIsConvertedToConnectionExceptionEvenWhenWithoutFactory()
2632+
{
2633+
$this->expectException(ConnectionException::class);
2634+
$this->expectExceptionMessage('cURL error 28: Operation timed out');
2635+
2636+
$pendingRequest = new PendingRequest();
2637+
2638+
$pendingRequest->setHandler(function () {
2639+
throw new GuzzleRequestException(
2640+
'cURL error 28: Operation timed out',
2641+
new GuzzleRequest('GET', 'https://timeout-laravel.example')
2642+
);
2643+
});
2644+
2645+
$pendingRequest->get('https://timeout-laravel.example');
2646+
}
2647+
2648+
public function testRequestExceptionWithResponseIsConvertedToConnectionExceptionEvenWhenWithoutFactory()
2649+
{
2650+
$this->expectException(ConnectionException::class);
2651+
$this->expectExceptionMessage('cURL error 28: Operation timed out');
2652+
2653+
$pendingRequest = new PendingRequest();
2654+
2655+
$pendingRequest->setHandler(function () {
2656+
throw new GuzzleRequestException(
2657+
'cURL error 28: Operation timed out',
2658+
new GuzzleRequest('GET', 'https://timeout-laravel.example'),
2659+
new Psr7Response(301)
2660+
);
2661+
});
2662+
2663+
$pendingRequest->get('https://timeout-laravel.example');
2664+
}
2665+
2666+
public function testTooManyRedirectsExceptionIsConvertedToConnectionExceptionEvenWhenWithoutFactory()
2667+
{
2668+
$this->expectException(ConnectionException::class);
2669+
$this->expectExceptionMessage('Maximum number of redirects (5) exceeded');
2670+
2671+
$pendingRequest = new PendingRequest();
2672+
2673+
$pendingRequest->setHandler(function () {
2674+
throw new TooManyRedirectsException(
2675+
'Maximum number of redirects (5) exceeded',
2676+
new GuzzleRequest('GET', 'https://redirect.laravel.example'),
2677+
new Psr7Response(301)
2678+
);
2679+
});
2680+
2681+
$pendingRequest->maxRedirects(5)->get('https://redirect.laravel.example');
2682+
}
2683+
26132684
public function testTooManyRedirectsExceptionConvertedToConnectionException()
26142685
{
26152686
$this->factory->fake(function () {

0 commit comments

Comments
 (0)