Skip to content

[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
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

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
hason committed Sep 17, 2015
commit e191df5597bb8e0b8966ff72b36b511cce7a4b07
1 change: 1 addition & 0 deletions src/Symfony/Component/Debug/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* added BufferingLogger for errors that happen before a proper logger is configured
* allow throwing from `__toString()` with `return trigger_error($e, E_USER_ERROR);`
* deprecate ExceptionHandler::createResponse
* added ExceptionFlattener

2.7.0
-----
Expand Down
53 changes: 50 additions & 3 deletions src/Symfony/Component/Debug/Exception/FlattenException.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class FlattenException extends LegacyFlattenException
private $headers;
private $file;
private $line;
private $extras = array();

public static function create(\Exception $exception, $statusCode = null, array $headers = array())
{
Expand Down Expand Up @@ -219,7 +220,8 @@ public function setTraceFromException(\Exception $exception)
public function setTrace($trace, $file, $line)
{
$this->trace = array();
$this->trace[] = array(

$this->trace[-1] = array(
'namespace' => '',
'short_class' => '',
'class' => '',
Expand All @@ -229,7 +231,8 @@ public function setTrace($trace, $file, $line)
'line' => $line,
'args' => array(),
);
foreach ($trace as $entry) {

foreach ($trace as $key => $entry) {
$class = '';
$namespace = '';
if (isset($entry['class'])) {
Expand All @@ -238,7 +241,7 @@ public function setTrace($trace, $file, $line)
$namespace = implode('\\', $parts);
}

$this->trace[] = array(
$this->trace[$key] = array(
'namespace' => $namespace,
'short_class' => $class,
'class' => isset($entry['class']) ? $entry['class'] : '',
Expand All @@ -251,6 +254,50 @@ public function setTrace($trace, $file, $line)
}
}

/**
* Replaces trace.
*
* @param array $trace The trace
*/
public function replaceTrace($trace)
Copy link
Member

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)?

{
$this->trace = (array) $trace;
}

/**
* Returns all extras.
*
* @return array
*/
public function getExtras()
{
return $this->extras;
}

/**
* Returns an extra value.
*
* @param string $name The name of the extra
* @param mixed $default The value to return if the extra doesn't exist
*
* @return mixed
*/
public function getExtra($name, $default = null)
{
return array_key_exists($name, $this->extras) ? $this->extras[$name] : $default;
}

/**
* Sets an extra value.
*
* @param string $name The name of the extra
* @param mixed $value The value
*/
public function setExtra($name, $value)
{
$this->extras[$name] = $value;
}

private function flattenArgs($args, $level = 0, &$count = 0)
{
$result = array();
Expand Down
88 changes: 88 additions & 0 deletions src/Symfony/Component/Debug/ExceptionFlattener.php
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 src/Symfony/Component/Debug/FlattenExceptionProcessorInterface.php
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public function testToArray(\Exception $exception, $statusCode)
array(
'message' => 'test',
'class' => 'Exception',
'trace' => array(array(
'trace' => array(-1 => array(
'namespace' => '', 'short_class' => '', 'class' => '', 'type' => '', 'function' => '', 'file' => 'foo.php', 'line' => 123,
'args' => array(),
)),
Expand Down Expand Up @@ -235,12 +235,12 @@ public function testSetTraceIncompleteClass()
'message' => 'test',
'class' => 'Exception',
'trace' => array(
array(
-1 => array(
'namespace' => '', 'short_class' => '', 'class' => '', 'type' => '', 'function' => '',
'file' => 'foo.php', 'line' => 123,
'args' => array(),
),
array(
0 => array(
'namespace' => '', 'short_class' => '', 'class' => '', 'type' => '', 'function' => 'test',
'file' => __FILE__, 'line' => 123,
'args' => array(
Expand Down
134 changes: 134 additions & 0 deletions src/Symfony/Component/Debug/Tests/ExceptionFlattenerTest.php
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'));
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy