From 5e7410a93011b751e98111c462eabf01af862149 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Sat, 29 Jan 2022 16:55:44 -0300 Subject: [PATCH] Improve Error message stacktrace style MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #17326 Signed-off-by: MaurĂ­cio Meneghini Fauth --- libraries/classes/Error.php | 42 ++++++++++++-------------------- templates/error/get_display.twig | 23 +++++++---------- test/classes/ErrorTest.php | 16 +++++++++--- 3 files changed, 37 insertions(+), 44 deletions(-) diff --git a/libraries/classes/Error.php b/libraries/classes/Error.php index ec6e3f6b53..b06b35a3ef 100644 --- a/libraries/classes/Error.php +++ b/libraries/classes/Error.php @@ -354,62 +354,52 @@ class Error extends Message */ public function getBacktraceDisplay(): string { - return self::formatBacktrace( - $this->getBacktrace(), - "
\n", - "
\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 = '
    '; foreach ($backtrace as $step) { + $retval .= '
  1. '; 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 .= '
  2. '; } - return $retval; + return $retval . '
'; } /** * 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 .= '
'; foreach ($step['args'] as $arg) { $retval .= "\t"; $retval .= $arg; - $retval .= ',' . $separator; + $retval .= ',
'; } } 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(), ]); } diff --git a/templates/error/get_display.twig b/templates/error/get_display.twig index 43e22a08e7..81a555a0e9 100644 --- a/templates/error/get_display.twig +++ b/templates/error/get_display.twig @@ -1,17 +1,12 @@ \ No newline at end of file + {%- if not is_user_error -%} +

Backtrace

+ {{- formatted_backtrace|raw -}} + {%- endif -%} + diff --git a/test/classes/ErrorTest.php b/test/classes/ErrorTest.php index 0f830ac5ee..2dab5dad49 100644 --- a/test/classes/ErrorTest.php +++ b/test/classes/ErrorTest.php @@ -120,7 +120,7 @@ class ErrorTest extends AbstractTestCase public function testGetBacktraceDisplay(): void { $this->assertStringContainsString( - 'PHPUnit\Framework\TestResult->run()
', + 'PHPUnit\Framework\TestResult->run()', $this->object->getBacktraceDisplay() ); } @@ -130,10 +130,18 @@ class ErrorTest extends AbstractTestCase */ public function testGetDisplay(): void { - $this->assertStringContainsString( - '