From bb020f4a729b7178ebabd26cbab6bb225deb3f09 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Fri, 3 Apr 2015 07:27:47 +0200 Subject: [PATCH] [HttpKernel] Embed the original exception as previous to bounced exceptions --- .../HttpKernel/EventListener/ExceptionListener.php | 13 ++++++++++++- .../Tests/EventListener/ExceptionListenerTest.php | 2 ++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/HttpKernel/EventListener/ExceptionListener.php b/src/Symfony/Component/HttpKernel/EventListener/ExceptionListener.php index c7f4fda12f..8db846d8b6 100644 --- a/src/Symfony/Component/HttpKernel/EventListener/ExceptionListener.php +++ b/src/Symfony/Component/HttpKernel/EventListener/ExceptionListener.php @@ -62,7 +62,18 @@ class ExceptionListener implements EventSubscriberInterface // set handling to false otherwise it wont be able to handle further more $handling = false; - // throwing $e, not $exception, is on purpose: fixing error handling code paths is the most important + $wrapper = $e; + + while ($prev = $wrapper->getPrevious()) { + if ($exception === $wrapper = $prev) { + throw $e; + } + } + + $prev = new \ReflectionProperty('Exception', 'previous'); + $prev->setAccessible(true); + $prev->setValue($wrapper, $exception); + throw $e; } diff --git a/src/Symfony/Component/HttpKernel/Tests/EventListener/ExceptionListenerTest.php b/src/Symfony/Component/HttpKernel/Tests/EventListener/ExceptionListenerTest.php index 86244df0fc..8fb00f51c1 100644 --- a/src/Symfony/Component/HttpKernel/Tests/EventListener/ExceptionListenerTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/EventListener/ExceptionListenerTest.php @@ -57,6 +57,7 @@ class ExceptionListenerTest extends \PHPUnit_Framework_TestCase $this->fail('RuntimeException expected'); } catch (\RuntimeException $e) { $this->assertSame('bar', $e->getMessage()); + $this->assertSame('foo', $e->getPrevious()->getMessage()); } } @@ -77,6 +78,7 @@ class ExceptionListenerTest extends \PHPUnit_Framework_TestCase $this->fail('RuntimeException expected'); } catch (\RuntimeException $e) { $this->assertSame('bar', $e->getMessage()); + $this->assertSame('foo', $e->getPrevious()->getMessage()); } $this->assertEquals(3, $logger->countErrors());