Skip to content

Commit cfae925

Browse files
committed
minor symfony#6948 Update docs for setting custom response code in exception handler (jameshalsall)
This PR was submitted for the master branch but it was merged into the 3.3 branch instead (closes symfony#6948). Discussion ---------- Update docs for setting custom response code in exception handler Docs update for the changes applied in symfony/symfony#19822 Commits ------- 5f0becf Update docs for setting custom response code in exception handler
2 parents 13a98ec + 5f0becf commit cfae925

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

reference/events.rst

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -217,22 +217,35 @@ sent as response::
217217
that forwards the ``Request`` to a given controller defined by the
218218
``exception_listener.controller`` parameter.
219219

220-
.. note::
220+
Symfony uses the following logic to determine the HTTP status code of the
221+
response:
222+
223+
* If :method:`Symfony\\Component\\HttpFoundation\\Response::isClientError`,
224+
:method:`Symfony\\Component\\HttpFoundation\\Response::isServerError` or
225+
:method:`Symfony\\Component\\HttpFoundation\\Response::isRedirect` is true,
226+
then the status code on your ``Response`` object is used;
227+
228+
* If the original exception implements
229+
:class:`Symfony\\Component\\HttpKernel\\Exception\\HttpExceptionInterface`,
230+
then ``getStatusCode()`` is called on the exception and used (the headers
231+
from ``getHeaders()`` are also added);
221232

222-
Symfony uses the following logic to determine the HTTP status code of the
223-
response:
233+
* If both of the above aren't true, then a 500 status code is used.
224234

225-
* If :method:`Symfony\\Component\\HttpFoundation\\Response::isClientError`,
226-
:method:`Symfony\\Component\\HttpFoundation\\Response::isServerError` or
227-
:method:`Symfony\\Component\\HttpFoundation\\Response::isRedirect` is true,
228-
then the status code on your ``Response`` object is used;
235+
.. note::
236+
237+
If you want to overwrite the status code of the exception response, which
238+
you should not without a good reason, call
239+
``GetResponseForExceptionEvent::allowSuccessfulResponse()`` first and then
240+
set the status code on the response::
229241

230-
* If the original exception implements
231-
:class:`Symfony\\Component\\HttpKernel\\Exception\\HttpExceptionInterface`,
232-
then ``getStatusCode()`` is called on the exception and used (the headers
233-
from ``getHeaders()`` are also added);
242+
$event->allowSuccessfulResponse();
243+
$response = new Response('No Content', 204);
244+
$event->setResponse($response);
234245

235-
* If both of the above aren't true, then a 500 status code is used.
246+
The status code sent to the client in the above example will be ``204`. If
247+
``$event->allowSuccessfulResponse()`` is omitted, then the kernel will set
248+
an appropriate status code based on the type of exception thrown.
236249

237250
.. seealso::
238251

0 commit comments

Comments
 (0)