Improve Error message stacktrace style

Fixes #17326

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
This commit is contained in:
Maurício Meneghini Fauth 2022-01-29 16:55:44 -03:00
parent 745c39bf9c
commit 5e7410a930
No known key found for this signature in database
GPG Key ID: 6A16FD38AFC89CC8
3 changed files with 37 additions and 44 deletions

View File

@ -354,62 +354,52 @@ class Error extends Message
*/
public function getBacktraceDisplay(): string
{
return self::formatBacktrace(
$this->getBacktrace(),
"<br>\n",
"<br>\n"
);
return self::formatBacktrace($this->getBacktrace());
}
/**
* return formatted backtrace field
*
* @param array $backtrace Backtrace data
* @param string $separator Arguments separator to use
* @param string $lines Lines separator to use
* @param array $backtrace Backtrace data
*
* @return string formatted backtrace
*/
public static function formatBacktrace(
array $backtrace,
string $separator,
string $lines
): string {
$retval = '';
public static function formatBacktrace(array $backtrace): string
{
$retval = '<ol class="list-group">';
foreach ($backtrace as $step) {
$retval .= '<li class="list-group-item">';
if (isset($step['file'], $step['line'])) {
$retval .= self::relPath($step['file'])
. '#' . $step['line'] . ': ';
$retval .= self::relPath($step['file']) . '#' . $step['line'] . ': ';
}
if (isset($step['class'])) {
$retval .= $step['class'] . $step['type'];
}
$retval .= self::getFunctionCall($step, $separator);
$retval .= $lines;
$retval .= self::getFunctionCall($step);
$retval .= '</li>';
}
return $retval;
return $retval . '</ol>';
}
/**
* Formats function call in a backtrace
*
* @param array $step backtrace step
* @param string $separator Arguments separator to use
* @param array $step backtrace step
*/
public static function getFunctionCall(array $step, string $separator): string
public static function getFunctionCall(array $step): string
{
$retval = $step['function'] . '(';
if (isset($step['args'])) {
if (count($step['args']) > 1) {
$retval .= $separator;
$retval .= '<br>';
foreach ($step['args'] as $arg) {
$retval .= "\t";
$retval .= $arg;
$retval .= ',' . $separator;
$retval .= ',<br>';
}
} elseif (count($step['args']) > 0) {
foreach ($step['args'] as $arg) {
@ -481,12 +471,12 @@ class Error extends Message
return $template->render('error/get_display', [
'context' => $context,
'isUserError' => $this->isUserError(),
'is_user_error' => $this->isUserError(),
'type' => $this->getType(),
'file' => $this->getFile(),
'line' => $this->getLine(),
'message' => $this->getMessage(),
'backtraceDisplay' => $this->getBacktraceDisplay(),
'formatted_backtrace' => $this->getBacktraceDisplay(),
]);
}

View File

@ -1,17 +1,12 @@
<div class="alert alert-{{ context }}" role="alert">
{%- if not is_user_error -%}
<p><strong>{{ type }}</strong> in {{ file }}#{{ line }}</p>
{%- endif -%}
{%- if isUserError == false %}
<strong>{{ type }}</strong> in {{ file ~'#'~ line }} <br>
{% endif -%}
{{ message|raw }}
{{ message }}
{% if isUserError == false %}
<br><br>
<strong>Backtrace</strong><br>
<br>
{{ backtraceDisplay }}
{% endif %}
</div>
{%- if not is_user_error -%}
<p class="mt-3"><strong>Backtrace</strong></p>
{{- formatted_backtrace|raw -}}
{%- endif -%}
</div>

View File

@ -120,7 +120,7 @@ class ErrorTest extends AbstractTestCase
public function testGetBacktraceDisplay(): void
{
$this->assertStringContainsString(
'PHPUnit\Framework\TestResult->run(<Class:PhpMyAdmin\Tests\ErrorTest>)<br>',
'PHPUnit\Framework\TestResult->run(<Class:PhpMyAdmin\Tests\ErrorTest>)',
$this->object->getBacktraceDisplay()
);
}
@ -130,10 +130,18 @@ class ErrorTest extends AbstractTestCase
*/
public function testGetDisplay(): void
{
$this->assertStringContainsString(
'<div class="alert alert-danger" role="alert"> <strong>Warning</strong>',
$this->object->getDisplay()
$actual = $this->object->getDisplay();
$this->assertStringStartsWith(
'<div class="alert alert-danger" role="alert"><p><strong>Warning</strong> in error.txt#15</p>'
. '<img src="themes/dot.gif" title="" alt="" class="icon ic_s_error"> Compile Error'
. '<p class="mt-3"><strong>Backtrace</strong></p><ol class="list-group"><li class="list-group-item">',
$actual
);
$this->assertStringContainsString(
'PHPUnit\Framework\TestResult->run(<Class:PhpMyAdmin\Tests\ErrorTest>)</li><li class="list-group-item">',
$actual
);
$this->assertStringEndsWith('</li></ol></div>' . "\n", $actual);
}
/**