diff --git a/src/Symfony/Component/HttpKernel/EventListener/ExceptionListener.php b/src/Symfony/Component/HttpKernel/EventListener/ExceptionListener.php index 74544a9fd1..a54fe45e9f 100644 --- a/src/Symfony/Component/HttpKernel/EventListener/ExceptionListener.php +++ b/src/Symfony/Component/HttpKernel/EventListener/ExceptionListener.php @@ -67,13 +67,13 @@ class ExceptionListener implements EventSubscriberInterface try { $response = $event->getKernel()->handle($request, HttpKernelInterface::SUB_REQUEST, true); } catch (\Exception $e) { - $this->logException($exception, sprintf('Exception thrown when handling an exception (%s: %s)', get_class($e), $e->getMessage()), false); + $this->logException($e, sprintf('Exception thrown when handling an exception (%s: %s at %s line %s)', get_class($e), $e->getMessage(), $e->getFile(), $e->getLine()), false); // set handling to false otherwise it wont be able to handle further more $handling = false; - // re-throw the exception from within HttpKernel as this is a catch-all - return; + // throwing $e, not $exception, is on purpose: fixing error handling code paths is the most important + throw $e; } $event->setResponse($response); @@ -91,7 +91,7 @@ class ExceptionListener implements EventSubscriberInterface /** * Logs an exception. * - * @param \Exception $exception The original \Exception instance + * @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 */ diff --git a/src/Symfony/Component/HttpKernel/Tests/EventListener/ExceptionListenerTest.php b/src/Symfony/Component/HttpKernel/Tests/EventListener/ExceptionListenerTest.php index 28f64e82c7..86244df0fc 100644 --- a/src/Symfony/Component/HttpKernel/Tests/EventListener/ExceptionListenerTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/EventListener/ExceptionListenerTest.php @@ -54,8 +54,9 @@ class ExceptionListenerTest extends \PHPUnit_Framework_TestCase try { $l->onKernelException($event2); - } catch (\Exception $e) { - $this->assertSame('foo', $e->getMessage()); + $this->fail('RuntimeException expected'); + } catch (\RuntimeException $e) { + $this->assertSame('bar', $e->getMessage()); } } @@ -73,8 +74,9 @@ class ExceptionListenerTest extends \PHPUnit_Framework_TestCase try { $l->onKernelException($event2); - } catch (\Exception $e) { - $this->assertSame('foo', $e->getMessage()); + $this->fail('RuntimeException expected'); + } catch (\RuntimeException $e) { + $this->assertSame('bar', $e->getMessage()); } $this->assertEquals(3, $logger->countErrors()); @@ -137,6 +139,6 @@ class TestKernelThatThrowsException implements HttpKernelInterface { public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true) { - throw new \Exception('bar'); + throw new \RuntimeException('bar'); } }