Skip to content

Commit 8f54ed9

Browse files
committed
Merge branch '4.4' into 5.0
* 4.4: [HttpClient] better explain how to deal with exceptions
2 parents e1ed6f1 + 5575645 commit 8f54ed9

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

components/http_client.rst

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,34 @@ When the HTTP status code of the response is in the 300-599 range (i.e. 3xx,
433433
// instead the original response content (even if it's an error message)
434434
$content = $response->getContent(false);
435435

436+
While responses are lazy, their destructor will always wait for headers to come
437+
back. This means that the following request *will* complete; and if e.g. a 404
438+
is returned, an exception will be thrown::
439+
440+
// because the returned value is not assigned to a variable, the destructor
441+
// of the returned response will be called immediately and will throw if the
442+
// status code is in the 300-599 range
443+
$client->request('POST', 'https://...');
444+
445+
This in turn means that unassigned responses will fallback to synchronous requests.
446+
If you want to make these requests concurrent, you can store their corresponding
447+
responses in an array::
448+
449+
$responses[] = $client->request('POST', 'https://.../path1');
450+
$responses[] = $client->request('POST', 'https://.../path2');
451+
// ...
452+
453+
// This line will trigger the destructor of all responses stored in the array;
454+
// they will complete concurrently and an exception will be thrown in case a
455+
// status code in the 300-599 range is returned
456+
unset($responses);
457+
458+
This behavior provided at destruction-time is part of the fail-safe design of the
459+
component. No errors will be unnoticed: if you don't write the code to handle
460+
errors, exceptions will notify you when needed. On the other hand, if you write
461+
the error-handling code, you will opt-out from these fallback mechanisms as the
462+
destructor won't have anything remaining to do.
463+
436464
Concurrent Requests
437465
-------------------
438466

@@ -567,7 +595,16 @@ catching ``TransportExceptionInterface`` in the foreach loop::
567595

568596
foreach ($client->stream($responses) as $response => $chunk) {
569597
try {
570-
if ($chunk->isLast()) {
598+
if ($chunk->isTimeout()) {
599+
// ... decide what to do when a timeout occurs
600+
// if you want to stop a response that timed out, don't miss
601+
// calling $response->cancel() or the destructor of the response
602+
// will try to complete it one more time
603+
} elseif ($chunk->isFirst()) {
604+
// if you want to check the status code, you must do it when the
605+
// first chunk arrived, using $response->getStatusCode();
606+
// not doing so might trigger an HttpExceptionInterface
607+
} elseif ($chunk->isLast()) {
571608
// ... do something with $response
572609
}
573610
} catch (TransportExceptionInterface $e) {

0 commit comments

Comments
 (0)