Skip to content

Commit 59dc108

Browse files
committed
[HttpClient] Check satus code before decoding in TraceableResponse::toArray()
1 parent a29af81 commit 59dc108

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

src/Symfony/Component/HttpClient/Response/TraceableResponse.php

+22-8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use Symfony\Component\HttpClient\Exception\ServerException;
1717
use Symfony\Component\HttpClient\TraceableHttpClient;
1818
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
19+
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
20+
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
1921
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
2022
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
2123
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
@@ -52,24 +54,36 @@ public function getHeaders(bool $throw = true): array
5254

5355
public function getContent(bool $throw = true): string
5456
{
55-
$this->content = $this->response->getContent(false);
56-
5757
if ($throw) {
58-
$this->checkStatusCode($this->response->getStatusCode());
58+
try {
59+
$this->checkStatusCode($this->response->getStatusCode());
60+
} catch (HttpExceptionInterface $httpException) {
61+
try {
62+
return $this->content = $this->response->getContent(false);
63+
} catch (TransportExceptionInterface $e) {
64+
throw $httpException ?? $e;
65+
}
66+
}
5967
}
6068

61-
return $this->content;
69+
return $this->content = $this->response->getContent(false);
6270
}
6371

6472
public function toArray(bool $throw = true): array
6573
{
66-
$this->content = $this->response->toArray(false);
67-
6874
if ($throw) {
69-
$this->checkStatusCode($this->response->getStatusCode());
75+
try {
76+
$this->checkStatusCode($this->response->getStatusCode());
77+
} catch (HttpExceptionInterface $httpException) {
78+
try {
79+
return $this->content = $this->response->toArray(false);
80+
} catch (DecodingExceptionInterface | TransportExceptionInterface $e) {
81+
throw $httpException ?? $e;
82+
}
83+
}
7084
}
7185

72-
return $this->content;
86+
return $this->content = $this->response->toArray(false);
7387
}
7488

7589
public function cancel(): void

src/Symfony/Component/HttpClient/Tests/TraceableHttpClientTest.php

+13
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\HttpClient\NativeHttpClient;
1717
use Symfony\Component\HttpClient\Response\MockResponse;
1818
use Symfony\Component\HttpClient\TraceableHttpClient;
19+
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
1920
use Symfony\Contracts\HttpClient\HttpClientInterface;
2021
use Symfony\Contracts\HttpClient\Test\TestHttpServer;
2122

@@ -100,4 +101,16 @@ public function testStream()
100101
$this->assertGreaterThan(1, \count($chunks));
101102
$this->assertSame('Symfony is awesome!', implode('', $chunks));
102103
}
104+
105+
public function testToArrayChecksStatusCodeBeforeDecoding()
106+
{
107+
$this->expectException(ClientExceptionInterface::class);
108+
109+
$sut = new TraceableHttpClient(new MockHttpClient($responseFactory = function (): MockResponse {
110+
return new MockResponse('Errored.', ['http_code' => 400]);
111+
}));
112+
113+
$response = $sut->request('GET', 'https://example.com/foo/bar');
114+
$response->toArray();
115+
}
103116
}

0 commit comments

Comments
 (0)