8000 [HttpKernel] Turn HTTP exceptions to responses on terminateWithExcept… · symfony/symfony@76d532e · GitHub
[go: up one dir, main page]

Skip to content

Commit 76d532e

Browse files
[HttpKernel] Turn HTTP exceptions to responses on terminateWithException()
1 parent 9a077ca commit 76d532e

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

src/Symfony/Bundle/FrameworkB 8000 undle/Resources/config/web.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
</service>
6969

7070
<service id="http_exception_listener" class="Symfony\Component\HttpKernel\EventListener\ExceptionListener">
71-
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" priority="-256"/>
71+
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" priority="-2048" />
7272
<tag name="monolog.logger" channel="request" />
7373
<argument>null</argument>
7474
<argument type="service" id="logger" on-invalid="null" />

src/Symfony/Component/HttpKernel/EventListener/ExceptionListener.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ public function __construct($controller, LoggerInterface $logger = null, $debug
4949

5050
public function onKernelException(GetResponseForExceptionEvent $event)
5151
{
52+
if (null === $this->controller) {
53+
$terminating = false;
54+
foreach (debug_backtrace() as $frame) {
55+
if (isset($frame['object']) && $event->getKernel() === $frame['object'] && 'terminateWithException' === $frame['function']) {
56+
$terminating = true;
57+
break;
58+
}
59+
}
60+
61+
if (!$terminating) {
62+
return;
63+
}
64+
}
5265
$exception = $event->getException();
5366
$request = $event->getRequest();
5467
$eventDispatcher = func_num_args() > 2 ? func_get_arg(2) : null;

src/Symfony/Component/HttpKernel/Tests/EventListener/ExceptionListenerTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\EventDispatcher\EventDispatcher;
1616
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
17+
use Symfony\Component\HttpKernel\HttpKernel;
1718
use Symfony\Component\HttpKernel\HttpKernelInterface;
1819
use Symfony\Component\HttpKernel\EventListener\ExceptionListener;
1920
use Symfony\Component\HttpKernel\KernelEvents;
@@ -155,7 +156,7 @@ public function testCSPHeaderIsRemoved()
155156
public function testNullController()
156157
{
157158
$listener = new ExceptionListener(null);
158-
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
159+
$kernel = $this->getMockBuilder(HttpKernel::class)->disableOriginalConstructor()->getMock();
159160
$kernel->expects($this->once())->method('handle')->will($this->returnCallback(function (Request $request) {
160161
$controller = $request->attributes->get('_controller');
161162

@@ -166,6 +167,12 @@ public function testNullController()
166167

167168
$listener->onKernelException($event);
168169

170+
$this->assertNull($event->getResponse());
171+
172+
$kernel->expects($this->once())->method('terminateWithException')->will($this->returnCallback(function () use ($listener, $event) {
173+
$listener->onKernelException($event);
174+
}));
175+
$kernel->terminateWithException(new \Exception('foo'));
169176
$this->assertContains('Whoops, looks like something went wrong.', $event->getResponse()->getContent());
170177
}
171178
}

0 commit comments

Comments
 (0)
0