Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d547ab9

Browse filesBrowse files
committed
Unwrap errors in FlattenException.
1 parent 7a461cf commit d547ab9
Copy full SHA for d547ab9

File tree

Expand file treeCollapse file tree

4 files changed

+87
-5
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+87
-5
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Debug/Exception/FatalThrowableError.php
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,14 @@
1818
*/
1919
class FatalThrowableError extends FatalErrorException
2020
{
21+
private $originalClassName;
22+
private $originalMessage;
23+
2124
public function __construct(\Throwable $e)
2225
{
26+
$this->originalClassName = \get_class($e);
27+
$this->originalMessage = $e->getMessage();
28+
2329
if ($e instanceof \ParseError) {
2430
$message = 'Parse error: '.$e->getMessage();
2531
$severity = E_PARSE;
@@ -41,4 +47,14 @@ public function __construct(\Throwable $e)
4147

4248
$this->setTrace($e->getTrace());
4349
}
50+
51+
public function getOriginalClassName(): string
52+
{
53+
return $this->originalClassName;
54+
}
55+
56+
public function getOriginalMessage(): string
57+
{
58+
return $this->originalMessage;
59+
}
4460
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Debug/Exception/FlattenException.php
+10-3Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +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());
4039
$e->setCode($exception->getCode());
4140

4241
if ($exception instanceof HttpExceptionInterface) {
@@ -53,7 +52,15 @@ public static function create(\Exception $exception, $statusCode = null, array $
5352
$e->setStatusCode($statusCode);
5453
$e->setHeaders($headers);
5554
$e->setTraceFromException($exception);
56-
$e->setClass(get_class($exception));
55+
56+
if ($exception instanceof FatalThrowableError) {
57+
$e->setMessage($exception->getOriginalMessage());
58+
$e->setClass($exception->getOriginalClassName());
59+
} else {
60+
$e->setMessage($exception->getMessage());
61+
$e->setClass(get_class($exception));
62+
}
63+
5764
$e->setFile($exception->getFile());
5865
$e->setLine($exception->getLine());
5966

@@ -157,7 +164,7 @@ public function getPrevious()
157164
return $this->previous;
158165
}
159166

160-
public function setPrevious(FlattenException $previous)
167+
public function setPrevious(self $previous)
161168
{
162169
$this->previous = $previous;
163170
}
+48Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Debug\Tests\Exception;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Debug\Exception\FatalThrowableError;
16+
17+
class FatalThrowableErrorTest extends TestCase
18+
{
19+
public function testDivisionByZeroError(): void
20+
{
21+
$error = new FatalThrowableError(new \DivisionByZeroError('Oops'));
22+
23+
$this->assertSame('Oops', $error->getMessage());
24+
$this->assertSame(E_ERROR, $error->getSeverity());
25+
$this->assertSame('DivisionByZeroError', $error->getOriginalClassName());
26+
$this->assertSame('Oops', $error->getOriginalMessage());
27+
}
28+
29+
public function testParseError(): void
30+
{
31+
$error = new FatalThrowableError(new \ParseError('Oops'));
32+
33+
$this->assertSame('Parse error: Oops', $error->getMessage());
34+
$this->assertSame(E_PARSE, $error->getSeverity());
35+
$this->assertSame('ParseError', $error->getOriginalClassName());
36+
$this->assertSame('Oops', $error->getOriginalMessage());
37+
}
38+
39+
public function testTypeError(): void
40+
{
41+
$error = new FatalThrowableError(new \TypeError('Oops'));
42+
43+
$this->assertSame('Type error: Oops', $error->getMessage());
44+
$this->assertSame(E_RECOVERABLE_ERROR, $error->getSeverity());
45+
$this->assertSame('TypeError', $error->getOriginalClassName());
46+
$this->assertSame('Oops', $error->getOriginalMessage());
47+
}
48+
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Debug/Tests/Exception/FlattenExceptionTest.php
+13-2Lines changed: 13 additions & 2 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;
@@ -123,6 +124,16 @@ public function testFlattenHttpException(\Exception $exception)
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
*/
@@ -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
/**

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.