feature #13780 [HttpKernel] Throw a LogicException when kernel.exception does not lead to a Response (nicolas-grekas)

This PR was merged into the 2.7 branch.

Discussion
----------

[HttpKernel] Throw a LogicException when kernel.exception does not lead to a Response

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Commits
-------

22f4807 [HttpKernel] Throw a LogicException when kernel.exception does not led to a Response
This commit is contained in:
Fabien Potencier 2015-03-14 06:13:34 +01:00
commit e5fe2dff01
4 changed files with 16 additions and 18 deletions

View File

@ -83,20 +83,15 @@ class ExceptionListener implements EventSubscriberInterface
*
* @param \Exception $exception The \Exception instance
* @param string $message The error message to log
* @param bool $original False when the handling of the exception thrown another exception
*/
protected function logException(\Exception $exception, $message, $original = true)
protected function logException(\Exception $exception, $message)
{
$isCritical = !$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500;
$context = array('exception' => $exception);
if (null !== $this->logger) {
if ($isCritical) {
$this->logger->critical($message, $context);
if (!$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500) {
$this->logger->critical($message, array('exception' => $exception));
} else {
$this->logger->error($message, $context);
$this->logger->error($message, array('exception' => $exception));
}
} elseif (!$original || $isCritical) {
error_log($message);
}
}

View File

@ -229,7 +229,7 @@ class HttpKernel implements HttpKernelInterface, TerminableInterface
if (!$event->hasResponse()) {
$this->finishRequest($request, $type);
throw $e;
throw new \LogicException('No listeners of the "kernel.exception" event set a Response', 0, $e);
}
$response = $event->getResponse();

View File

@ -102,8 +102,8 @@ class ContainerAwareHttpKernelTest extends \PHPUnit_Framework_TestCase
$this->fail('->handle() suppresses the controller exception');
} catch (\PHPUnit_Framework_Exception $exception) {
throw $exception;
} catch (\Exception $actual) {
$this->assertSame($expected, $actual, '->handle() throws the controller exception');
} catch (\LogicException $actual) {
$this->assertSame($expected, $actual->getPrevious(), '->handle() throws the controller exception, wrapped when no listener');
}
}

View File

@ -23,14 +23,17 @@ use Symfony\Component\EventDispatcher\EventDispatcher;
class HttpKernelTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \RuntimeException
*/
public function testHandleWhenControllerThrowsAnExceptionAndRawIsTrue()
{
$kernel = new HttpKernel(new EventDispatcher(), $this->getResolver(function () { throw new \RuntimeException(); }));
$exception = new \RuntimeException();
$kernel = new HttpKernel(new EventDispatcher(), $this->getResolver(function () use ($exception) { throw $exception; }));
$kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true);
try {
$kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true);
$this->fail('LogicException expected');
} catch (\LogicException $e) {
$this->assertSame($exception, $e->getPrevious());
}
}
/**
@ -132,7 +135,7 @@ class HttpKernelTest extends \PHPUnit_Framework_TestCase
$dispatcher = new EventDispatcher();
$kernel = new HttpKernel($dispatcher, $this->getResolver(false));
$kernel->handle(new Request());
$kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, false);
}
/**