8000 feature #26028 Unwrap errors in FlattenException (derrabus) · symfony/symfony@ade7970 · GitHub
[go: up one dir, main page]

Skip to content

Commit ade7970

Browse files
feature #26028 Unwrap errors in FlattenException (derrabus)
This PR was merged into the 4.1-dev branch. Discussion ---------- Unwrap errors in FlattenException | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | maybe | Deprecations? | no | Tests pass? | no (but probably unrelated?) | Fixed tickets | #26025 | License | MIT | Doc PR | N/A This is probably the most straightforward way to solve #26025. `FlattenException` is now unwrapping `FatalThrowableError` instances and logs the wrapped error instead. The consequence of this change is that the real error class is displayend on TwigBundle's exception page and the profiler. Regarding BC: If we assume that `FlattenException` is used for rendering and logging, everything should be fine. But this PR changes `FlattenException`'s internal behavior. If a piece of code relied on errors appearing `FatalThrowableError` inside a `FlattenException`, that code would break. <img width="402" alt="bildschirmfoto 2018-02-02 um 20 08 42" src="https://user-images.githubusercontent.com/1506493/35760077-0b202940-087e-11e8-9b98-8e4ba269780c.png"> Commits ------- f14d7d6 Unwrap errors in FlattenException.
2 parents d2fb4d1 + f14d7d6 commit ade7970

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,22 @@
1818
*/
1919
class FatalThrowableError extends FatalErrorException
2020
{
21+
private $originalClassName;
22+
2123
public function __construct(\Throwable $e)
2224
{
25+
$this->originalClassName = \get_class($e);
26+
2327
if ($e instanceof \ParseError) {
24-
$message = 'Parse error: '.$e->getMessage();
2528
$severity = E_PARSE;
2629
} elseif ($e instanceof \TypeError) {
27-
$message = 'Type error: '.$e->getMessage();
2830
$severity = E_RECOVERABLE_ERROR;
2931
} else {
30-
$message = $e->getMessage();
3132
$severity = E_ERROR;
3233
}
3334

3435
\ErrorException::__construct(
35-
$message,
36+
$e->getMessage(),
3637
$e->getCode(),
3738
$severity,
3839
$e->getFile(),
@@ -41,4 +42,9 @@ public function __construct(\Throwable $e)
4142

4243
$this->setTrace($e->getTrace());
4344
}
45+
46+
public function getOriginalClassName(): string
47+
{
48+
return $this->originalClassName;
49+
}
4450
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static function create(\Exception $exception, $statusCode = null, array $
5353
$e->setStatusCode($statusCode);
5454
$e->setHeaders($headers);
5555
$e->setTraceFromException($exception);
56-
$e->setClass(get_class($exception));
56+
$e->setClass($exception instanceof FatalThrowableError ? $exception->getOriginalClassName() : \get_class($exception));
5757
$e->setFile($exception->getFile());
5858
$e->setLine($exception->getLine());
5959

@@ -157,7 +157,7 @@ public function getPrevious()
157157
return $this->previous;
158158
}
159159

160-
public function setPrevious(FlattenException $previous)
160+
public function setPrevious(self $previous)
161161
{
162162
$this->previous = $previous;
163163
}

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

Lines 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