Skip to content

[HttpKernel] Handle throwable errors in HttpKernel #26514

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

Closed
wants to merge 1 commit into from
Closed
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
22 changes: 12 additions & 10 deletions src/Symfony/Component/HttpKernel/HttpKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\HttpKernel;

use Symfony\Component\Debug\Exception\FatalThrowableError;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
Expand Down Expand Up @@ -64,7 +65,7 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ

try {
return $this->handleRaw($request, $type);
} catch (\Exception $e) {
} catch (\Throwable $e) {
if ($e instanceof RequestExceptionInterface) {
$e = new BadRequestHttpException($e->getMessage(), $e);
}
Expand All @@ -74,7 +75,7 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
throw $e;
}

return $this->handleException($e, $request, $type);
return $this->handleThrowable($e, $request, $type);
}
}

Expand All @@ -95,7 +96,7 @@ public function terminateWithException(\Exception $exception, Request $request =
throw $exception;
}

$response = $this->handleException($exception, $request, self::MASTER_REQUEST);
$response = $this->handleThrowable($exception, $request, self::MASTER_REQUEST);

$response->sendHeaders();
$response->sendContent();
Expand Down Expand Up @@ -207,19 +208,20 @@ private function finishRequest(Request $request, int $type)
/**
* Handles an exception by trying to convert it to a Response.
*
* @param \Exception $e An \Exception instance
* @param Request $request A Request instance
* @param int $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
* @param int $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
*
* @throws \Exception
*/
private function handleException(\Exception $e, Request $request, int $type): Response
private function handleThrowable(\Throwable $e, Request $request, int $type): Response
{
$event = new GetResponseForExceptionEvent($this, $request, $type, $e);
$eventException = $e instanceof \Exception ? $e : new FatalThrowableError($e);

$event = new GetResponseForExceptionEvent($this, $request, $type, $eventException);
$this->dispatcher->dispatch(KernelEvents::EXCEPTION, $event);

// a listener might have replaced the exception
$e = $event->getException();
$processedException = $event->getException();
$e = $eventException === $processedException ? $e : $processedException;

if (!$event->hasResponse()) {
$this->finishRequest($request, $type);
Expand All @@ -243,7 +245,7 @@ private function handleException(\Exception $e, Request $request, int $type): Re

try {
return $this->filterResponse($response, $request, $type);
} catch (\Exception $e) {
} catch (\Throwable $e) {
return $response;
}
}
Expand Down
95 changes: 95 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/HttpKernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\HttpKernel\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Debug\Exception\FatalThrowableError;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
Expand Down Expand Up @@ -111,6 +112,100 @@ public function testHandleHttpException()
$this->assertEquals('POST', $response->headers->get('Allow'));
}

public function testUnhandledError()
{
$expectedError = new \DivisionByZeroError('Ouch');

$kernel = $this->getHttpKernel(new EventDispatcher(), function () use ($expectedError) {
throw $expectedError;
});

$actualError = null;
try {
$kernel->handle(new Request());
} catch (\Throwable $actualError) {
}

$this->assertSame($expectedError, $actualError);
}

public function testReplacedError()
{
$expectedException = new \RuntimeException('Replacement');

$dispatcher = new EventDispatcher();
$dispatcher->addListener(KernelEvents::EXCEPTION, function (GetResponseForExceptionEvent $event) use ($expectedException) {
$event->setException($expectedException);
});

$kernel = $this->getHttpKernel($dispatcher, function () {
throw new \DivisionByZeroError('Ouch');
});

$actualException = null;
try {
$kernel->handle(new Request());
} catch (\Throwable $actualException) {
}

$this->assertSame($expectedException, $actualException);
}

public function testHandleError()
{
$dispatcher = new EventDispatcher();
$dispatcher->addListener(KernelEvents::EXCEPTION, function (GetResponseForExceptionEvent $event) {
$this->assertInstanceOf(FatalThrowableError::class, $event->getException());
$event->setResponse(new Response($event->getException()->getMessage()));
});

$kernel = $this->getHttpKernel($dispatcher, function () {
throw new \DivisionByZeroError('Ouch');
});
$response = $kernel->handle(new Request());

$this->assertEquals('500', $response->getStatusCode());
$this->assertEquals('Ouch', $response->getContent());
}

public function testResponseFilterThrowsAnotherException()
{
$dispatcher = new EventDispatcher();
$dispatcher->addListener(KernelEvents::EXCEPTION, function (GetResponseForExceptionEvent $event) {
$event->setResponse(new Response($event->getException()->getMessage()));
});
$dispatcher->addListener(KernelEvents::RESPONSE, function () {
throw new \RuntimeException('Ouch');
});

$kernel = $this->getHttpKernel($dispatcher, function () {
throw new \RuntimeException('Ouch');
});
$response = $kernel->handle(new Request());

$this->assertEquals('500', $response->getStatusCode());
$this->assertEquals('Ouch', $response->getContent());
}

public function testResponseFilterRaisesErrorAfterException()
{
$dispatcher = new EventDispatcher();
$dispatcher->addListener(KernelEvents::EXCEPTION, function (GetResponseForExceptionEvent $event) {
$event->setResponse(new Response($event->getException()->getMessage()));
});
$dispatcher->addListener(KernelEvents::RESPONSE, function () {
throw new \DivisionByZeroError('Ouch');
});

$kernel = $this->getHttpKernel($dispatcher, function () {
throw new \RuntimeException('Ouch');
});
$response = $kernel->handle(new Request());

$this->assertEquals('500', $response->getStatusCode());
$this->assertEquals('Ouch', $response->getContent());
}

public function getStatusCodes()
{
return array(
Expand Down