8000 [HttpKernel] Introduce `ExceptionEvent::isKernelTerminating()` to ski… · symfony/symfony@68673a3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 68673a3

Browse files
VincentLangletfabpot
authored andcommitted
[HttpKernel] Introduce ExceptionEvent::isKernelTerminating() to skip error rendering when kernel is terminating
1 parent 923ecdb commit 68673a3

File tree

5 files changed

+40
-3
lines changed

5 files changed

+40
-3
lines changed

UPGRADE-7.1.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
UPGRADE FROM 7.0 to 7.1
2+
=======================
3+
4+
HttpKernel
5+
----------
6+
7+
* `ExceptionEvent` now takes an optional `$isKernelTerminating` parameter

src/Symfony/Component/HttpKernel/Event/ExceptionEvent.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ final class ExceptionEvent extends RequestEvent
3131
{
3232
private \Throwable $throwable;
3333
private bool $allowCustomResponseCode = false;
34+
private bool $isKernelTerminating = false;
3435

35-
public function __construct(HttpKernelInterface $kernel, Request $request, int $requestType, \Throwable $e)
36+
public function __construct(HttpKernelInterface $kernel, Request $request, int $requestType, \Throwable $e, bool $isKernelTerminating = false)
3637
{
3738
parent::__construct($kernel, $request, $requestType);
3839

3940
$this->setThrowable($e);
41+
$this->isKernelTerminating = $isKernelTerminating;
4042
}
4143

4244
public function getThrowable(): \Throwable
@@ -69,4 +71,9 @@ public function isAllowingCustomResponseCode(): bool
6971
{
7072
return $this->allowCustomResponseCode;
7173
}
74+
75+
public function isKernelTerminating(): bool
76+
{
77+
return $this->isKernelTerminating;
78+
}
7279
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ public function onKernelException(ExceptionEvent $event)
102102
return;
103103
}
104104

105+
if (!$this->debug && $event->isKernelTerminating()) {
106+
return;
107+
}
108+
105109
$throwable = $event->getThrowable();
106110

107111
if ($exceptionHandler = set_exception_handler(var_dump(...))) {

src/Symfony/Component/HttpKernel/HttpKernel.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class HttpKernel implements HttpKernelInterface, TerminableInterface
5656
protected $requestStack;
5757
private ArgumentResolverInterface $argumentResolver;
5858
private bool $handleAllThrowables;
59+
private bool $terminating = false;
5960

6061
public function __construct(EventDispatcherInterface $dispatcher, ControllerResolverInterface $resolver, RequestStack $requestStack = null, ArgumentResolverInterface $argumentResolver = null, bool $handleAllThrowables = false)
6162
{
@@ -112,7 +113,12 @@ public function handle(Request $request, int $type = HttpKernelInterface::MAIN_R
112113
*/
113114
public function terminate(Request $request, Response $response)
114115
{
115-
$this->dispatcher->dispatch(new TerminateEvent($this, $request, $response), KernelEvents::TERMINATE);
116+
try {
117+
$this->terminating = true;
118+
$this->dispatcher->dispatch(new TerminateEvent($this, $request, $response), KernelEvents::TERMINATE);
119+
} finally {
120+
$this->terminating = false;
121+
}
116122
}
117123

118124
/**
@@ -235,7 +241,7 @@ private function finishRequest(Request $request, int $type): void
235241
*/
236242
private function handleThrowable(\Throwable $e, Request $request, int $type): Response
237243
{
238-
$event = new ExceptionEvent($this, $request, $type, $e);
244+
$event = new ExceptionEvent($this, $request, $type, $e, isKernelTerminating: $this->terminating);
239245
$this->dispatcher->dispatch($event, KernelEvents::EXCEPTION);
240246

241247
// a listener might have replaced the exception

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,19 @@ public function testCSPHeaderIsRemoved()
244244
$this->assertFalse($response->headers->has('content-security-policy'), 'CSP header has been removed');
245245
}
246246

247+
public function testTerminating()
248+
{
249+
$listener = new ErrorListener('foo', $this->createMock(LoggerInterface::class));
250+
251+
$kernel = $this->createMock(HttpKernelInterface::class);
252+
$kernel->expects($this->never())->method('handle');
253+
254+
$request = Request::create('/');
255+
256+
$event = new ExceptionEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, new \Exception('foo'), true);
257+
$listener->onKernelException($event);
258+
}
259+
247260
/**
248261
* @dataProvider controllerProvider
249262
*/

0 commit comments

Comments
 (0)
0