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

This commit is contained in:
Nicolas Grekas 2015-02-24 19:55:41 +01:00
parent 2b036ecbb5
commit 22f4807522
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 \Exception $exception The \Exception instance
* @param string $message The error message to log * @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 (null !== $this->logger) {
if ($isCritical) { if (!$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500) {
$this->logger->critical($message, $context); $this->logger->critical($message, array('exception' => $exception));
} else { } 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()) { if (!$event->hasResponse()) {
$this->finishRequest($request, $type); $this->finishRequest($request, $type);
throw $e; throw new \LogicException('No listeners of the "kernel.exception" event set a Response', 0, $e);
} }
$response = $event->getResponse(); $response = $event->getResponse();

View File

@ -102,8 +102,8 @@ class ContainerAwareHttpKernelTest extends \PHPUnit_Framework_TestCase
$this->fail('->handle() suppresses the controller exception'); $this->fail('->handle() suppresses the controller exception');
} catch (\PHPUnit_Framework_Exception $exception) { } catch (\PHPUnit_Framework_Exception $exception) {
throw $exception; throw $exception;
} catch (\Exception $actual) { } catch (\LogicException $actual) {
$this->assertSame($expected, $actual, '->handle() throws the controller exception'); $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 class HttpKernelTest extends \PHPUnit_Framework_TestCase
{ {
/**
* @expectedException \RuntimeException
*/
public function testHandleWhenControllerThrowsAnExceptionAndRawIsTrue() 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(); $dispatcher = new EventDispatcher();
$kernel = new HttpKernel($dispatcher, $this->getResolver(false)); $kernel = new HttpKernel($dispatcher, $this->getResolver(false));
$kernel->handle(new Request()); $kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, false);
} }
/** /**