Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpKernel] Introduce ExceptionEvent::isKernelTerminating() to skip error rendering when kernel is terminating #52128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions UPGRADE-7.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
UPGRADE FROM 7.0 to 7.1
=======================

HttpKernel
----------

* `ExceptionEvent` now takes an optional `$isKernelTerminating` parameter
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we add this to the CHANGELOG instead to the upgrade guide, as this is an optional one?

9 changes: 8 additions & 1 deletion src/Symfony/Component/HttpKernel/Event/ExceptionEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ final class ExceptionEvent extends RequestEvent
{
private \Throwable $throwable;
private bool $allowCustomResponseCode = false;
private bool $isKernelTerminating = false;

public function __construct(HttpKernelInterface $kernel, Request $request, int $requestType, \Throwable $e)
public function __construct(HttpKernelInterface $kernel, Request $request, int $requestType, \Throwable $e, bool $isKernelTerminating = false)
{
parent::__construct($kernel, $request, $requestType);

$this->setThrowable($e);
$this->isKernelTerminating = $isKernelTerminating;
}

public function getThrowable(): \Throwable
Expand Down Expand Up @@ -69,4 +71,9 @@ public function isAllowingCustomResponseCode(): bool
{
return $this->allowCustomResponseCode;
}

public function isKernelTerminating(): bool
{
return $this->isKernelTerminating;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ public function onKernelException(ExceptionEvent $event)
return;
}

if (!$this->debug && $event->isKernelTerminating()) {
return;
}

$throwable = $event->getThrowable();

if ($exceptionHandler = set_exception_handler(var_dump(...))) {
Expand Down
10 changes: 8 additions & 2 deletions src/Symfony/Component/HttpKernel/HttpKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class HttpKernel implements HttpKernelInterface, TerminableInterface
protected $requestStack;
private ArgumentResolverInterface $argumentResolver;
private bool $handleAllThrowables;
private bool $terminating = false;

public function __construct(EventDispatcherInterface $dispatcher, ControllerResolverInterface $resolver, RequestStack $requestStack = null, ArgumentResolverInterface $argumentResolver = null, bool $handleAllThrowables = false)
{
Expand Down Expand Up @@ -112,7 +113,12 @@ public function handle(Request $request, int $type = HttpKernelInterface::MAIN_R
*/
public function terminate(Request $request, Response $response)
{
$this->dispatcher->dispatch(new TerminateEvent($this, $request, $response), KernelEvents::TERMINATE);
try {
$this->terminating = true;
$this->dispatcher->dispatch(new TerminateEvent($this, $request, $response), KernelEvents::TERMINATE);
} finally {
$this->terminating = false;
}
}

/**
Expand Down Expand Up @@ -235,7 +241,7 @@ private function finishRequest(Request $request, int $type): void
*/
private function handleThrowable(\Throwable $e, Request $request, int $type): Response
{
$event = new ExceptionEvent($this, $request, $type, $e);
$event = new ExceptionEvent($this, $request, $type, $e, isKernelTerminating: $this->terminating);
$this->dispatcher->dispatch($event, KernelEvents::EXCEPTION);

// a listener might have replaced the exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,19 @@ public function testCSPHeaderIsRemoved()
$this->assertFalse($response->headers->has('content-security-policy'), 'CSP header has been removed');
}

public function testTerminating()
{
$listener = new ErrorListener('foo', $this->createMock(LoggerInterface::class));

$kernel = $this->createMock(HttpKernelInterface::class);
$kernel->expects($this->never())->method('handle');

$request = Request::create('/');

$event = new ExceptionEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, new \Exception('foo'), true);
$listener->onKernelException($event);
}

/**
* @dataProvider controllerProvider
*/
Expand Down