-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[WIP] Better exception page #15792
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
[WIP] Better exception page #15792
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e191df5
[Debug] Added ExceptionFlattener
hason bab9b5c
Integrated the ExceptionFlattener into components and bundles
hason 0f558bc
[Twig] Added an exception processor for TwigError
hason 8f4ab6a
[HttpKernel] Added exception processor for HTTP Exceptions
hason 74f7357
[Debug] Added an exception processor for dumping arguments
hason 2e9ac2d
[Debug] [TwigBundle] Added syntax highlighters for PHP and Twig
hason File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
[Debug] Added ExceptionFlattener
- Loading branch information
commit e191df5597bb8e0b8966ff72b36b511cce7a4b07
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Debug; | ||
|
||
use Symfony\Component\Debug\Exception\FlattenException; | ||
|
||
/** | ||
* ExceptionFlattener converts an Exception to FlattenException. | ||
* | ||
* @author Martin Hasoň <martin.hason@gmail.com> | ||
*/ | ||
class ExceptionFlattener | ||
{ | ||
/** | ||
* @var FlattenExceptionProcessorInterface[] | ||
*/ | ||
private $processors = array(); | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param array $processors The collection of processors | ||
*/ | ||
public function __construct($processors = array()) | ||
{ | ||
foreach ($processors as $processor) { | ||
$this->addProcessor($processor); | ||
} | ||
} | ||
|
||
/** | ||
* Adds an exception processor. | ||
* | ||
* @param FlattenExceptionProcessorInterface $processor | ||
*/ | ||
public function addProcessor(FlattenExceptionProcessorInterface $processor) | ||
{ | ||
$this->processors[] = $processor; | ||
} | ||
|
||
/** | ||
* Flattens an exception. | ||
* | ||
* @param \Exception $exception The raw exception | ||
* | ||
* @return FlattenException | ||
*/ | ||
public function flatten(\Exception $exception) | ||
{ | ||
$exceptions = array(); | ||
do { | ||
$exceptions[] = $exception; | ||
} while ($exception = $exception->getPrevious()); | ||
|
||
$previous = null; | ||
foreach (array_reverse($exceptions, true) as $position => $exception) { | ||
$e = new FlattenException(); | ||
$e->setMessage($exception->getMessage()); | ||
$e->setCode($exception->getCode()); | ||
$e->setClass(get_class($exception)); | ||
$e->setFile($exception->getFile()); | ||
$e->setLine($exception->getLine()); | ||
$e->setTraceFromException($exception); | ||
if (null !== $previous) { | ||
$e->setPrevious($previous); | ||
} | ||
|
||
foreach ($this->processors as $processor) { | ||
if ($newE = $processor->process($exception, $e, 0 === $position)) { | ||
$e = $newE; | ||
} | ||
} | ||
|
||
$previous = $e; | ||
} | ||
|
||
return $e; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Symfony/Component/Debug/FlattenExceptionProcessorInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Debug; | ||
|
||
use Symfony\Component\Debug\Exception\FlattenException; | ||
|
||
/** | ||
* @author Martin Hasoň <martin.hason@gmail.com> | ||
*/ | ||
interface FlattenExceptionProcessorInterface | ||
{ | ||
/** | ||
* Process a flattened exception. | ||
* | ||
* @param \Exception $exception The raw exception | ||
* @param FlattenException $flattenException The flattened exception | ||
* @param bool $master Whether it is a master exception | ||
* | ||
* @return FlattenException | ||
*/ | ||
public function process(\Exception $exception, FlattenException $flattenException, $master); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
src/Symfony/Component/Debug/Tests/ExceptionFlattenerTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Debug\Tests; | ||
|
||
use Symfony\Component\Debug\Exception\FlattenException; | ||
use Symfony\Component\Debug\ExceptionFlattener; | ||
use Symfony\Component\Debug\FlattenExceptionProcessorInterface; | ||
|
||
class ExceptionFlattenerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
private $flattener; | ||
|
||
protected function setUp() | ||
{ | ||
$this->flattener = new ExceptionFlattener(); | ||
} | ||
|
||
public function testFlattenException() | ||
{ | ||
$exception = new \RuntimeException('Runtime exception'); | ||
$flattened = $this->flattener->flatten($exception); | ||
|
||
$this->assertEquals($exception->getMessage(), $flattened->getMessage()); | ||
$this->assertEquals($exception->getCode(), $flattened->getCode()); | ||
$this->assertEquals($exception->getFile(), $flattened->getFile()); | ||
$this->assertEquals($exception->getLine(), $flattened->getLine()); | ||
$this->assertInstanceOf($flattened->getClass(), $exception); | ||
} | ||
|
||
public function testFlattenPreviousException() | ||
{ | ||
$exception1 = new \OutOfRangeException('Out of range exception'); | ||
$exception2 = new \InvalidArgumentException('Invalid argument exception', null, $exception1); | ||
$exception3 = new \RuntimeException('Runtime exception', null, $exception2); | ||
|
||
$flattened = $this->flattener->flatten($exception3); | ||
$this->assertCount(2, $flattened->getAllPrevious()); | ||
$this->assertInstanceOf('Symfony\Component\Debug\Exception\FlattenException', $flattened->getPrevious()); | ||
$this->assertInstanceOf( | ||
'Symfony\Component\Debug\Exception\FlattenException', | ||
$flattened->getPrevious()->getPrevious() | ||
); | ||
} | ||
|
||
public function testFlattenWithProcessor() | ||
{ | ||
$this->flattener->addProcessor(new TagTraceProcessor()); | ||
|
||
$exception = new \RuntimeException('Runtime exception'); | ||
$flattened = $this->flattener->flatten($exception); | ||
foreach ($flattened->getTrace() as $position => $entry) { | ||
if (-1 === $position) { | ||
$this->assertFalse(array_key_exists('tag', $entry)); | ||
} else { | ||
$this->assertArrayHasKey('tag', $entry); | ||
} | ||
} | ||
} | ||
|
||
public function testProcessorReplaceException() | ||
{ | ||
$this->flattener->addProcessor(new EmptyExceptionProcessor()); | ||
|
||
$exception = new \RuntimeException('Runtime exception'); | ||
$flattened = $this->flattener->flatten($exception); | ||
|
||
$this->assertNull($flattened->getMessage()); | ||
$this->assertNull($flattened->getCode()); | ||
$this->assertNull($flattened->getFile()); | ||
$this->assertNull($flattened->getLine()); | ||
} | ||
|
||
public function testProcessOnlyMaterException() | ||
{ | ||
$exception1 = new \OutOfRangeException('Out of range exception'); | ||
$exception2 = new \InvalidArgumentException('Invalid argument exception', null, $exception1); | ||
$exception3 = new \RuntimeException('Runtime exception', null, $exception2); | ||
|
||
$this->flattener->addProcessor(new MasterExtraProcessor()); | ||
$flattened = $this->flattener->flatten($exception3); | ||
|
||
$this->assertEquals(array('tags' => array('master')), $flattened->getExtras()); | ||
foreach ($flattened->getAllPrevious() as $exception) { | ||
$this->assertEquals(array(), $exception->getExtras()); | ||
} | ||
} | ||
} | ||
|
||
class TagTraceProcessor implements FlattenExceptionProcessorInterface | ||
{ | ||
public function process(\Exception $exception, FlattenException $flattenException, $master) | ||
{ | ||
$trace = $flattenException->getTrace(); | ||
|
||
foreach ($exception->getTrace() as $key => $entry) { | ||
if (!isset($trace[$key])) { | ||
continue; | ||
} | ||
|
||
$trace[$key]['tag'] = 'value'; | ||
} | ||
|
||
$flattenException->replaceTrace($trace); | ||
} | ||
} | ||
|
||
class EmptyExceptionProcessor implements FlattenExceptionProcessorInterface | ||
{ | ||
public function process(\Exception $exception, FlattenException $flattenException, $master) | ||
{ | ||
return new FlattenException(); | ||
} | ||
} | ||
|
||
class MasterExtraProcessor implements FlattenExceptionProcessorInterface | ||
{ | ||
public function process(\Exception $exception, FlattenException $flattenException, $master) | ||
{ | ||
if (!$master) { | ||
return; | ||
} | ||
|
||
$flattenException->setExtra('tags', array('master')); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about adding an
array
type hint instead of casting to(array)
?