Skip to content

Commit 80567cc

Browse files
committed
minor #16141 [HttpClient] Rewording when an exception is thrown (ThomasLandauer)
This PR was merged into the 4.4 branch. Discussion ---------- [HttpClient] Rewording when an exception is thrown `@nicolas`-grekas Identical copy of #14499 Commits ------- dbc1270 Rewording when an exception is thrown
2 parents 08548d1 + dbc1270 commit 80567cc

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

http_client.rst

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -937,20 +937,19 @@ There are three types of exceptions, all of which implement the
937937
are thrown when a content-type cannot be decoded to the expected representation.
938938

939939
When the HTTP status code of the response is in the 300-599 range (i.e. 3xx,
940-
4xx or 5xx) your code is expected to handle it. If you don't do that, the
941-
``getHeaders()``, ``getContent()`` and ``toArray()`` methods throw an appropriate exception, which will
942-
implement the :class:`Symfony\\Contracts\\HttpClient\\Exception\\HttpExceptionInterface`::
940+
4xx or 5xx), the ``getHeaders()``, ``getContent()`` and ``toArray()`` methods
941+
throw an appropriate exception, all of which implement the
942+
:class:`Symfony\\Contracts\\HttpClient\\Exception\\HttpExceptionInterface`.
943943

944-
// the response of this request will be a 403 HTTP error
945-
$response = $client->request('GET', 'https://httpbin.org/status/403');
944+
To opt-out from this exception and deal with 300-599 status codes on your own,
945+
pass ``false`` as the optional argument to every call of those methods,
946+
e.g. ``$response->getHeaders(false);``.
946947

947-
// this code results in a Symfony\Component\HttpClient\Exception\ClientException
948-
// because it doesn't check the status code of the response
949-
$content = $response->getContent();
948+
If you do not call any of these 3 methods at all, the exception will still be thrown
949+
when the ``$response`` object is destructed.
950950

951-
// pass FALSE as the optional argument to not throw an exception and return
952-
// instead the original response content (even if it's an error message)
953-
$content = $response->getContent(false);
951+
Calling ``$response->getStatusCode()`` is enough to disable this behavior
952+
(but then don't miss checking the status code yourself).
954953

955954
While responses are lazy, their destructor will always wait for headers to come
956955
back. This means that the following request *will* complete; and if e.g. a 404

0 commit comments

Comments
 (0)