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 cfae925

Browse filesBrowse files
committed
minor #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 #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
Copy full SHA for cfae925

File tree

1 file changed

+25
-12
lines changed
Filter options

1 file changed

+25
-12
lines changed

‎reference/events.rst

Copy file name to clipboardExpand all lines: reference/events.rst
+25-12Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.