8000 [HttpClient] fix throwing HTTP exceptions when the 1st chunk is emitted · symfony/symfony@3c93764 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3c93764

Browse files
[HttpClient] fix throwing HTTP exceptions when the 1st chunk is emitted
1 parent 9d882e8 commit 3c93764

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -317,20 +317,28 @@ public static function stream(iterable $responses, float $timeout = null): \Gene
317317
} elseif ($chunk instanceof ErrorChunk) {
318318
unset($responses[$j]);
319319
$isTimeout = true;
320-
} elseif ($chunk instanceof FirstChunk && $response->logger) {
321-
$info = $response->getInfo();
322-
$response->logger->info(sprintf('Response: "%s %s"', $info['http_code'], $info['url']));
320+
} elseif ($chunk instanceof FirstChunk) {
321+
if ($response->logger) {
322+
$info = $response->getInfo();
323+
$response->logger->info(sprintf('Response: "%s %s"', $info['http_code'], $info['url']));
324+
}
325+
326+
yield $response => $chunk;
327+
328+
if ($response->initializer && null === $response->info['error']) {
329+
// Ensure the HTTP status code is always checked
330+
$response->getHeaders(true);
331+
}
332+
333+
continue;
323334
}
324335

325336
yield $response => $chunk;
326337
}
327338

328339
unset($multi->handlesActivity[$j]);
329340

330-
if ($chunk instanceof FirstChunk && null === $response->initializer && null === $response->info['error']) {
331-
// Ensure the HTTP status code is always checked
332-
$response->getHeaders(true);
333-
} elseif ($chunk instanceof ErrorChunk && !$chunk->didThrow()) {
341+
if ($chunk instanceof ErrorChunk && !$chunk->didThrow()) {
334342
// Ensure transport exceptions are always thrown
335343
$chunk->getContent();
336344
}

src/Symfony/Contracts/HttpClient/Test/HttpClientTestCase.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,16 @@ public function testClientError()
152152
$this->assertSame(404, $response->getStatusCode());
153153
$this->assertSame(['application/json'], $response->getHeaders(false)['content-type']);
154154
$this->assertNotEmpty($response->getContent(false));
155+
156+
$response = $client->request('GET', 'http://localhost:8057/404');
157+
158+
try {
159+
foreach ($client->stream($response) as $chunk) {
160+
$this->assertTrue($chunk->isFirst());
161+
}
162+
$this->fail(ClientExceptionInterface::class.' expected');
163+
} catch (ClientExceptionInterface $e) {
164+
}
155165
}
156166

157167
public function testIgnoreErrors()

0 commit comments

Comments
 (0)
0