Skip to content

Commit ea38d7f

Browse files
[HttpClient] better explain how to deal with exceptions
1 parent a65abca commit ea38d7f

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
@@ -449,6 +449,34 @@ When the HTTP status code of the response is in the 300-599 range (i.e. 3xx,
449449
// instead the original response content (even if it's an error message)
450450
$content = $response->getContent(false);
451451

452+
While responses are lazy, their destructor will always wait for headers to come
453+
back. This means that if the following request *will* complete; and if e.g. a 404
454+
is returned, an exception will be thrown::
455+
456+
// because the returned value is not assigned to a variable, the destructor
457+
// of the returned response will be called immediately and will throw if the
458+
// status code is in the 300-599 range
459+
$client->request('POST', 'https://...');
460+
461+
This in turn means that unassigned responses will fallback to synchronous requests.
462+
If you want to make these requests concurrent, you can store their corresponding
463+
responses in an array::
464+
465+
$responses[] = $client->request('POST', 'https://.../path1');
466+
$responses[] = $client->request('POST', 'https://.../path2');
467+
// ...
468+
469+
// This line will trigger the destructor of all responses stored in the array;
470+
// they will complete concurrently and an exception will be thrown in case a
471+
// status code in the 300-599 range is returned
472+
unset($responses);
473+
474+
This behavior provided at destruction-time is part of the fail-safe design of the
475+
component. No errors will be unnoticed: if you don't write the code to handle
476+
errors, exceptions will notify you when needed. On the other hand, if you write
477+
the error-handling code, you will opt-out from these fallback mecanisms as the
478+
destructor won't have anything remaining to do.
479+
452480
Concurrent Requests
453481
-------------------
454482

@@ -583,7 +611,16 @@ catching ``TransportExceptionInterface`` in the foreach loop::
583611

584612
foreach ($client->stream($responses) as $response => $chunk) {
585613
try {
586-
if ($chunk->isLast()) {
614+
if ($chunk->isTimeout()) {
615+
// ... decide what to do when a timeout occurs
616+
// if you want to stop a response that timed out, don't miss
617+
// calling $response->cancel() or the destructor of the response
618+
// will try to complete it one more time
619+
} elseif ($chunk->isFirst()) {
620+
// if you want to check the status code, you must do it when the
621+
// first chunk arrived, using $response->getStatusCode();
622+
// not doing so might trigger an HttpExceptionInterface
623+
} elseif ($chunk->isLast()) {
587624
// ... do something with $response
588625
}
589626
} catch (TransportExceptionInterface $e) {

0 commit comments

Comments
 (0)