Skip to content

Commit d269056

Browse files
committed
[symfony#1849] Adding exception handling that checks for HttpExceptionInterface
1 parent b35ca9a commit d269056

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

cookbook/service_container/event_listener.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ event is just one of the core kernel events::
1919

2020
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
2121
use Symfony\Component\HttpFoundation\Response;
22+
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
2223

2324
class AcmeExceptionListener
2425
{
@@ -31,7 +32,15 @@ event is just one of the core kernel events::
3132
// Customize our response object to display our exception details
3233
$response = new Response();
3334
$response->setContent($message);
34-
$response->setStatusCode(500);
35+
36+
// HttpExceptionInterface is a special type of exception that
37+
// holds status code and header details
38+
if ($exception instanceof HttpExceptionInterface) {
39+
$response->setStatusCode($exception->getStatusCode());
40+
$response->headers->replace($exception->getHeaders());
41+
} else {
42+
$response->setStatusCode(500);
43+
}
3544

3645
// Send our modified response object to the event
3746
$event->setResponse($response);

0 commit comments

Comments
 (0)