Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1f7b8fd

Browse filesBrowse files
bug #38378 [HttpClient] Fix exception triggered with AsyncResponse (jderusse)
This PR was merged into the 5.2-dev branch. Discussion ---------- [HttpClient] Fix exception triggered with AsyncResponse | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | #38289 (comment) | License | MIT | Doc PR | / When a request is replayed with the AsyncResponse and the 2nd request failed, users get an exception even when they pass `$throw=false`. In fact, there are 2 exceptions: 1. Cannot get the content of the response twice: buffering is disabled. https://github.com/symfony/symfony/blob/73ca97c97ab4c4fc9a7bca5fb4339fca22e0cfa1/src/Symfony/Component/HttpClient/Response/CommonResponseTrait.php#L51-L68 Because CommonResponseTrait::$content is null and `self::stream` yield only the LastChunk. The content is stays null 2. HTTP/2 429 returned for "https://httpbin.org/status/429" https://github.com/symfony/symfony/blob/73ca97c97ab4c4fc9a7bca5fb4339fca22e0cfa1/src/Symfony/Component/HttpClient/Response/AsyncResponse.php#L209-L225 Because on the second request passthru is null, it didn't disable the the exception thrown on destruct for the wrapped response. This Commits ------- 3aedb51 [HttpClient] Fix exception triggered with AsyncResponse
2 parents a3e8c11 + 3aedb51 commit 1f7b8fd
Copy full SHA for 1f7b8fd

File tree

2 files changed

+21
-5
lines changed
Filter options

2 files changed

+21
-5
lines changed

‎src/Symfony/Component/HttpClient/Response/AsyncResponse.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/Response/AsyncResponse.php
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ public static function stream(iterable $responses, float $timeout = null, string
206206
foreach ($client->stream($wrappedResponses, $timeout) as $response => $chunk) {
207207
$r = $asyncMap[$response];
208208

209+
if (null === $chunk->getError() && $chunk->isFirst()) {
210+
// Ensure no exception is thrown on destruct for the wrapped response
211+
$r->response->getStatusCode();
212+
}
213+
209214
if (!$r->passthru) {
210215
if (null !== $chunk->getError() || $chunk->isLast()) {
211216
unset($asyncMap[$response]);
@@ -219,11 +224,6 @@ public static function stream(iterable $responses, float $timeout = null, string
219224
continue;
220225
}
221226

222-
if (null === $chunk->getError() && $chunk->isFirst()) {
223-
// Ensure no exception is thrown on destruct for the wrapped response
224-
$r->response->getStatusCode();
225-
}
226-
227227
foreach (self::passthru($r->client, $r, $chunk, $asyncMap) as $chunk) {
228228
yield $r => $chunk;
229229
}

‎src/Symfony/Component/HttpClient/Tests/AsyncDecoratorTraitTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/Tests/AsyncDecoratorTraitTest.php
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,22 @@ public function testRetry404()
6262
$this->assertSame(200, $response->getStatusCode());
6363
}
6464

65+
public function testRetry404WithThrow()
66+
{
67+
$client = $this->getHttpClient(__FUNCTION__, function (ChunkInterface $chunk, AsyncContext $context) {
68+
$this->assertTrue($chunk->isFirst());
69+
$this->assertSame(404, $context->getStatusCode());
70+
$context->getResponse()->cancel();
71+
$context->replaceRequest('GET', 'http://localhost:8057/404');
72+
$context->passthru();
73+
});
74+
75+
$response = $client->request('GET', 'http://localhost:8057/404');
76+
77+
$this->expectException(ClientExceptionInterface::class);
78+
$response->getContent(true);
79+
}
80+
6581
public function testRetryTransportError()
6682
{
6783
$client = $this->getHttpClient(__FUNCTION__, function (ChunkInterface $chunk, AsyncContext $context) {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.