10000 [WIP] Better exception page by hason · Pull Request #15792 · symfony/symfony · GitHub
[go: up one dir, main page]

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
Prev Previous commit
Next Next commit
[Debug] Added an exception processor for dumping arguments
  • Loading branch information
hason committed Sep 17, 2015
commit 74f7357ce29a9b1b9108d8ecef4928aaa840ad63
43 changes: 13 additions & 30 deletions src/Symfony/Bridge/Twig/Extension/CodeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/

namespace Symfony\Bridge\Twig\Extension;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\Debug\Utils\HtmlUtils;

/**
* Twig extension relate to PHP code and used by the profiler and the default exception templates.
Expand All @@ -21,6 +23,7 @@ class CodeExtension extends \Twig_Extension
private $fileLinkFormat;
private $rootDir;
private $charset;
private $htmlUtils;

/**
* Constructor.
Expand All @@ -29,11 +32,12 @@ class CodeExtension extends \Twig_Extension
* @param string $rootDir The project root directory
* @param string $charset The charset
*/
public function __construct($fileLinkFormat, $rootDir, $charset)
public function __construct($fileLinkFormat, $rootDir, $charset, HtmlUtils $htmlUtils = null)
{
$this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
$this->rootDir = str_replace('/', DIRECTORY_SEPARATOR, dirname($rootDir)).DIRECTORY_SEPARATOR;
$this->charset = $charset;
$this->htmlUtils = $htmlUtils;
}

/**
Expand Down Expand Up @@ -78,48 +82,27 @@ public function abbrMethod($method)
/**
* Formats an array as a string.
*
* @param array $args The argument array
* @param array $args The argument array
* @param FlattenException|null $exception The flatten exception
*
* @return string
*/
public function formatArgs($args)
public function formatArgs($args, $exception = null)
{
$result = array();
foreach ($args as $key => $item) {
if ('object' === $item[0]) {
$parts = explode('\\', $item[1]);
$short = array_pop($parts);
$formattedValue = sprintf('<em>object</em>(<abbr title="%s">%s</abbr>)', $item[1], $short);
} elseif ('array' === $item[0]) {
$formattedValue = sprintf('<em>array</em>(%s)', is_array($item[1]) ? $this->formatArgs($item[1]) : $item[1]);
} elseif ('string' === $item[0]) {
$formattedValue = sprintf("'%s'", htmlspecialchars($item[1], ENT_QUOTES, $this->charset));
} elseif ('null' === $item[0]) {
$formattedValue = '<em>null</em>';
} elseif ('boolean' === $item[0]) {
$formattedValue = '<em>'.strtolower(var_export($item[1], true)).'</em>';
} elseif ('resource' === $item[0]) {
$formattedValue = '<em>resource</em>';
} else {
$formattedValue = str_replace("\n", '', var_export(htmlspecialchars((string) $item[1], ENT_QUOTES, $this->charset), true));
}

$result[] = is_int($key) ? $formattedValue : sprintf("'%s' => %s", $key, $formattedValue);
}

return implode(', ', $result);
return $this->htmlUtils->formatArgs($args, $exception);
}

/**
* Formats an array as a string.
*
* @param array $args The argument array
* @param array $args The argument array
* @param FlattenException|null $exception The flatten exception
*
* @return string
*/
public function formatArgsAsText($args)
public function formatArgsAsText($args, $exception = null)
{
return strip_tags($this->formatArgs($args));
return strip_tags($this->formatArgs($args, $exception));
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/Symfony/Bundle/DebugBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@
<tag name="exception.processor" />
</service>

<service id="debug.exception_processor.arguments" class="Symfony\Component\Debug\ArgumentsFlattenExceptionProcessor" public="false">
<tag name="exception.processor" />
<argument type="service" id="var_dumper.cloner" />
</service>

<service id="debug.html_utils" class="Symfony\Component\Debug\Utils\HtmlUtils">
</service>
</services>

</container>
1 change: 1 addition & 0 deletions src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
<argument /> <!-- %templating.helper.code.file_link_format% -->
<argument>%kernel.root_dir%</argument>
<argument>%kernel.charset%</argument>
<argument type="service" id="debug.html_utils" on-invalid="ignore" />
</service>

<service id="twig.extension.routing" class="%twig.extension.routing.class%" public="false">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
</div>
</div>

{% for position, e in exception.toarray %}
{% for position, e in [exception]|merge(exception.allprevious) %}
{% include 'TwigBundle:Exception:traces.html.twig' with { 'exception': e, 'position': position, 'count': previous_count } only %}
{% endfor %}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="{{ _charset }}" ?>

<error code="{{ status_code }}" message="{{ status_text }}">
{% for e in exception.toarray %}
{% for e in [exception]|merge(exception.allprevious) %}
<exception class="{{ e.class }}" message="{{ e.message }}">
{% include 'TwigBundle:Exception:traces.xml.twig' with { 'exception': e } only %}
</exception>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<abbr title="{{ trace.class }}">{{ trace.short_class }}</abbr>
{{ trace.type ~ trace.function }}
</strong>
({{ trace.args|format_args }})
({{ trace.args|format_args(exception|default) }})
{% endif %}

{% if trace.file is defined and trace.file and trace.line is defined and trace.line %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% if trace.function %}
at {{ trace.class ~ trace.type ~ trace.function }}({{ trace.args|format_args_as_text }})
at {{ trace.class ~ trace.type ~ trace.function }}({{ trace.args|format_args_as_text(exception|default) }})
{% else %}
at n/a
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<ol class="traces list-exception" id="traces-{{ position }}" style="display: {{ 0 == count ? 'block' : 'none' }}">
{% for i, trace in exception.trace %}
<li>
{% include 'TwigBundle:Exception:trace.html.twig' with { 'prefix': position, 'i': i, 'trace': trace } only %}
{% include 'TwigBundle:Exception:trace.html.twig' with { exception: exception, 'prefix': position, 'i': i, 'trace': trace } only %}
</li>
{% endfor %}
</ol>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% if exception.trace|length %}
{% for trace in exception.trace %}
{% include 'TwigBundle:Exception:trace.txt.twig' with { 'trace': trace } only %}
{% include 'TwigBundle:Exception:trace.txt.twig' with { exception: exception, 'trace': trace } only %}

{% endfor %}
{% endif %}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<traces>
{% for trace in exception.trace %}
<trace>
{% include 'TwigBundle:Exception:trace.txt.twig' with { 'trace': trace } only %}
{% include 'TwigBundle:Exception:trace.txt.twig' with { exception: exception, 'trace': trace } only %}

</trace>
{% endfor %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</h2>

<div id="traces-text" class="trace" style="display: none;">
<pre>{% for i, e in exception.toarray %}
<pre>{% for i, e in [exception]|merge(exception.allprevious) %}
[{{ i + 1 }}] {{ e.class }}: {{ e.message }}
{% include 'TwigBundle:Exception:traces.txt.twig' with { 'exception': e } only %}
{% endfor %}</pre>
Expand Down
168 changes: 168 additions & 0 deletions src/Symfony/Component/Debug/ArgumentsFlattenExceptionProcessor.php
2851
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?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;
use Symfony\Component\VarDumper\Cloner\ClonerInterface;

/**
* @author Martin Hasoň <martin.hason@gmail.com>
*/
class ArgumentsFlattenExceptionProcessor implements FlattenExceptionProcessorInterface
{
private $cloner;
private $shareArguments;

public function __construct(ClonerInterface $cloner = null, $shareArguments = true)
{
$this->cloner = $cloner;
$this->shareArguments = $shareArguments;
}

/**
* {@inheritdoc}
*/
public function process(\Exception $exception, FlattenException $flattenException, $master)
{
if (!$master) {
return;
}

$variables = array();
$values = array();

$e = $exception;
$f = $flattenException;

do {
$trace = $f->getTrace();
foreach ($e->getTrace() as $key => $entry) {
if (!isset($entry['args']) || !isset($trace[$key])) {
continue;
}

$parameters = $this->getParameters($entry);

$arguments = array();
foreach ($entry['args'] as $position => $argument) {
$link = array_search($argument, $variables, true);

if (false === $link) {
$link = hash('md5', uniqid(mt_rand(), true), false);
$variables[$link] = $argument;
$values[$link] = $this->shareArguments ? array('link', $link) : $this->flatten($argument);
}

if (isset($parameters[$position])) {
$arguments[$parameters[$position]->getName()] = $values[$link];
} else {
$arguments[] = $values[$link];
}
}

$trace[$key]['args'] = $arguments;
}
$f->replaceTrace($trace);
} while (($e = $e->getPrevious()) && ($f = $f->getPrevious()));

if (!$this->shareArguments) {
return;
}

$flattenVariables = $this->flatten($variables);
foreach (array_merge(array($flattenException), $flattenException->getAllPrevious()) as $f) {
$f->setExtra('trace_arguments', $flattenVariables);
}
}

private function getParameters($entry)
{
if (!isset($entry['function'])) {
return array();
}

try {
if (isset($entry['class'])) {
$ref = new \ReflectionMethod($entry['class'], $entry['function']);
} else {
$ref = new \ReflectionFunction($entry['function']);
}
} catch (\ReflectionException $e) {
return array();
}

return $ref->getParameters();
}

private function flatten($variable)
{
if (null === $this->cloner) {
return $this->flattenValue($variable);
} else {
return $this->cloner->cloneVar($variable);
}
}

private function flattenValue($value, $level = 0, &$count = 0)
{
if ($count++ > 1e4) {
return array('array', '*SKIPPED over 10000 entries*');
}

if (is_object($value)) {
return array('object', get_class($value));
}

if (is_array($value)) {
if ($level > 10) {
return array('array', '*DEEP NESTED ARRAY*');
}

$array = array();
foreach ($value as $k => $v) {
$array[$k] = $this->flattenValue($v, $level + 1, $count);
}

return array('array', $array);
}

if (null === $value) {
return array('null', null);
}

if (is_bool($value)) {
return array('boolean', $value);
}

if (is_float($value) || is_int($value)) {
return array('number', $value);
}

if (is_resource($value)) {
return array('resource', get_resource_type($value));
}

if ($value instanceof \__PHP_Incomplete_Class) {
// Special case of object, is_object will return false
return array('incomplete-object', $this->getClassNameFromIncomplete($value));
}

return array('string', (string) $value);
}

private function getClassNameFromIncomplete(\__PHP_Incomplete_Class $value)
{
$array = new \ArrayObject($value);

return $array['__PHP_Incomplete_Class_Name'];
}
}
Loading
0