8000 feature #52128 [HttpKernel] Introduce `ExceptionEvent::isKernelTermin… · symfony/symfony@638f7d0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 638f7d0

Browse files
committed
feature #52128 [HttpKernel] Introduce ExceptionEvent::isKernelTerminating() to skip error rendering when kernel is terminating (VincentLanglet)
This PR was squashed before being merged into the 7.1 branch. Discussion ---------- [HttpKernel] Introduce `ExceptionEvent::isKernelTerminating()` to skip error rendering when kernel is terminating | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #51912 | License | MIT Commits ------- 68673a3 [HttpKernel] Introduce `ExceptionEvent::isKernelTerminating()` to skip error rendering when kernel is terminating
2 parents 8e0f304 + 68673a3 commit 638f7d0

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
@@ -96,6 +96,10 @@ public function onKernelException(ExceptionEvent $event): void
9696
return;
9797
}
9898

99+
if (!$this->debug && $event->isKernelTerminating()) {
100+
return;
101+
}
102+
99103
$throwable = $event->getThrowable();
100104

101105
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 $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
{
@@ -109,7 +110,12 @@ public function handle(Request $request, int $type = HttpKernelInterface::MAIN_R
109110

110111
public function terminate(Request $request, Response $response): void
111112
{
112-
$this->dispatcher->dispatch(new TerminateEvent($this, $request, $response), KernelEvents::TERMINATE);
113+
try {
114+
$this->terminating = true;
115+
$this->dispatcher->dispatch(new TerminateEvent($this, $request, $response), KernelEvents::TERMINATE);
116+
} finally {
117+
$this->terminating = false;
118+
}
113119
}
114120

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

238244
// 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