8000 bug #17052 [2.7] Fixed flatten exception recursion with errors (Graha… · symfony/symfony@af3f4ea · GitHub
[go: up one dir, main page]

Skip to content

Commit af3f4ea

Browse files
committed
bug #17052 [2.7] Fixed flatten exception recursion with errors (GrahamCampbell)
This PR was submitted for the 2.7 branch but it was merged into the 2.3 branch instead (closes #17052). Discussion ---------- [2.7] Fixed flatten exception recursion with errors | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | N/A | License | MIT | Doc PR | N/A Consider the following code: ```php $error = new ParseError(); $exception = new RuntimeException($error->getMessage(), $error->getCode(), $error); $flat = new Symfony\Component\Debug\Exception\FlattenException($exception); ``` Without this fix, that code is broken. You'll end up with something like this: ``` FatalThrowableError in FlattenException.php line 76: Type error: Argument 1 passed to Symfony\Component\Debug\Exception\FlattenException::create() must be an instance of Exception, instance of ParseError given ``` --- I came across this error issue in laravel/framework#11329. Commits ------- 2b0721d [2.7] Fixed flatten exception recursion with errors
2 parents 021ab8a + 2b0721d commit af3f4ea

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/Symfony/Component/Debug/Exception/FlattenException.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,13 @@ public static function create(\Exception $exception, $statusCode = null, array $
9494
$e->setClass(get_class($exception));
9595
$e->setFile($exception->getFile());
9696
$e->setLine($exception->getLine());
97-
if ($exception->getPrevious()) {
98-
$e->setPrevious(static::create($exception->getPrevious()));
97+
98+
$previous = $exception->getPrevious();
99+
100+
if ($previous instanceof \Exception) {
101+
$e->setPrevious(static::create($previous));
102+
} elseif ($previous instanceof \Throwable) {
103+
$e->setPrevious(static::create(new FatalThrowableError($previous)));
99104
}
100105

101106
return $e;

src/Symfony/Component/Debug/Tests/Exception/FlattenExceptionTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,20 @@ public function testPrevious(\Exception $exception, $statusCode)
131131
$this->assertSame(array($flattened2), $flattened->getAllPrevious());
132132
}
133133

134+
/**
135+
* @requires PHP 7.0
136+
*/
137+
public function testPreviousError()
138+
{
139+
$exception = new \Exception('test', 123, new \ParseError('Oh noes!', 42));
140+
141+
$flattened = FlattenException::create($exception)->getPrevious();
142+
143+
$this->assertEquals($flattened->getMessage(), 'Parse error: Oh noes!', 'The message is copied from the original exception.');
144+
$this->assertEquals($flattened->getCode(), 42, 'The code is copied from the original exception.');
145+
$this->assertEquals($flattened->getClass(), 'Symfony\Component\Debug\Exception\FatalThrowableError', 'The class is set to the class of the original exception');
146+
}
147+
134148
/**
135149
* @dataProvider flattenDataProvider
136150
*/

0 commit comments

Comments
 (0)
0