Skip to content

Commit c63a3d1

Browse files
committed
Unwrap errors in FlattenException.
1 parent db10ee7 commit c63a3d1

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@
1818
*/
1919
class FatalThrowableError extends FatalErrorException
2020
{
21+
private $throwable;
22+
2123
public function __construct(\Throwable $e)
2224
{
25+
$this->throwable = $e;
26+
2327
if ($e instanceof \ParseError) {
2428
$message = 'Parse error: '.$e->getMessage();
2529
$severity = E_PARSE;
@@ -41,4 +45,9 @@ public function __construct(\Throwable $e)
4145

4246
$this->setTrace($e->getTrace());
4347
}
48+
49+
public function getThrowable(): \Throwable
50+
{
51+
return $this->throwable;
52+
}
4453
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ class FlattenException
3636
public static function create(\Exception $exception, $statusCode = null, array $headers = array())
3737
{
3838
$e = new static();
39-
$e->setMessage($exception->getMessage());
40-
$e->setCode($exception->getCode());
4139

4240
if ($exception instanceof HttpExceptionInterface) {
4341
$statusCode = $exception->getStatusCode();
@@ -53,6 +51,13 @@ public static function create(\Exception $exception, $statusCode = null, array $
5351
$e->setStatusCode($statusCode);
5452
$e->setHeaders($headers);
5553
$e->setTraceFromException($exception);
54+
55+
if ($exception instanceof FatalThrowableError) {
56+
$exception = $exception->getThrowable();
57+
}
58+
59+
$e->setMessage($exception->getMessage());
60+
$e->setCode($exception->getCode());
5661
$e->setClass(get_class($exception));
5762
$e->setFile($exception->getFile());
5863
$e->setLine($exception->getLine());
@@ -157,7 +162,7 @@ public function getPrevious()
157162
return $this->previous;
158163
}
159164

160-
public function setPrevious(FlattenException $previous)
165+
public function setPrevious(self $previous)
161166
{
162167
$this->previous = $previous;
163168
}

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Debug\Tests\Exception;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Debug\Exception\FatalThrowableError;
1516
use Symfony\Component\Debug\Exception\FlattenException;
1617
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
1718
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
@@ -111,7 +112,7 @@ public function testHeadersForHttpException()
111112
/**
112113
* @dataProvider flattenDataProvider
113114
*/
114-
public function testFlattenHttpException(\Exception $exception, $statusCode)
115+
public function testFlattenHttpException(\Exception $exception)
115116
{
116117
$flattened = FlattenException::create($exception);
117118
$flattened2 = FlattenException::create($exception);
@@ -123,10 +124,20 @@ public function testFlattenHttpException(\Exception $exception, $statusCode)
123124
$this->assertInstanceOf($flattened->getClass(), $exception, 'The class is set to the class of the original exception');
124125
}
125126

127+
public function testThrowable()
128+
{
129+
$exception = new FatalThrowableError(new \DivisionByZeroError('Ouch', 42));
130+
$flattened = FlattenException::create($exception);
131+
132+
$this->assertSame('Ouch', $flattened->getMessage(), 'The message is copied from the original error.');
133+
$this->assertSame(42, $flattened->getCode(), 'The code is copied from the original error.');
134+
$this->assertSame('DivisionByZeroError', $flattened->getClass(), 'The class is set to the class of the original error');
135+
}
136+
126137
/**
127138
* @dataProvider flattenDataProvider
128139
*/
129-
public function testPrevious(\Exception $exception, $statusCode)
140+
public function testPrevious(\Exception $exception)
130141
{
131142
$flattened = FlattenException::create($exception);
132143
$flattened2 = FlattenException::create($exception);
@@ -144,9 +155,9 @@ public function testPreviousError()
144155

145156
$flattened = FlattenException::create($exception)->getPrevious();
146157

147-
$this->assertEquals($flattened->getMessage(), 'Parse error: Oh noes!', 'The message is copied from the original exception.');
158+
$this->assertEquals($flattened->getMessage(), 'Oh noes!', 'The message is copied from the original exception.');
148159
$this->assertEquals($flattened->getCode(), 42, 'The code is copied from the original exception.');
149-
$this->assertEquals($flattened->getClass(), 'Symfony\Component\Debug\Exception\FatalThrowableError', 'The class is set to the class of the original exception');
160+
$this->assertEquals($flattened->getClass(), 'ParseError', 'The class is set to the class of the original exception');
150161
}
151162

152163
/**
@@ -170,7 +181,7 @@ public function testFile(\Exception $exception)
170181
/**
171182
* @dataProvider flattenDataProvider
172183
*/
173-
public function testToArray(\Exception $exception, $statusCode)
184+
public function testToArray(\Exception $exception)
174185
{
175186
$flattened = FlattenException::create($exception);
176187
$flattened->setTrace(array(), 'foo.php', 123);
@@ -190,7 +201,7 @@ public function testToArray(\Exception $exception, $statusCode)
190201
public function flattenDataProvider()
191202
{
192203
return array(
193-
array(new \Exception('test', 123), 500),
204+
array(new \Exception('test', 123)),
194205
);
195206
}
196207

@@ -253,7 +264,7 @@ function () {},
253264

254265
// assertEquals() does not like NAN values.
255266
$this->assertEquals($array[$i][0], 'float');
256-
$this->assertTrue(is_nan($array[$i++][1]));
267+
$this->assertNan($array[$i][1]);
257268
}
258269

259270
public function testRecursionInArguments()

0 commit comments

Comments
 (0)