diff --git a/lib/internal/Magento/Framework/App/ExceptionHandler.php b/lib/internal/Magento/Framework/App/ExceptionHandler.php index d1bd09da256a..120f9c415c22 100644 --- a/lib/internal/Magento/Framework/App/ExceptionHandler.php +++ b/lib/internal/Magento/Framework/App/ExceptionHandler.php @@ -1,8 +1,9 @@ $exception->getFile(), + 'line' => $exception->getLine() + ]); + } + /** * Build content based on an exception * @@ -126,19 +148,21 @@ private function buildContentFromException(\Exception $exception): string foreach ($exceptions as $index => $exception) { $buffer .= sprintf( - "Exception #%d (%s): %s\n", + "Exception #%d (%s): \"%s\" thrown at [%s]\n", $index, get_class($exception), - $exception->getMessage() + $exception->getMessage(), + $this->getExceptionLocation($exception) ); } foreach ($exceptions as $index => $exception) { $buffer .= sprintf( - "\nException #%d (%s): %s\n%s\n", + "\nException #%d (%s): \"%s\" thrown at [%s]\n%s\n", $index, get_class($exception), $exception->getMessage(), + $this->getExceptionLocation($exception), Debug::trace( $exception->getTrace(), true, diff --git a/lib/internal/Magento/Framework/Debug.php b/lib/internal/Magento/Framework/Debug.php index 6e5222a19884..067a77598a86 100644 --- a/lib/internal/Magento/Framework/Debug.php +++ b/lib/internal/Magento/Framework/Debug.php @@ -1,8 +1,9 @@ '/var/www/magento2/app/code/Magento/Test/Block/Test.php', + * 'line' => 1 + * ] + * + * Example Output: + * "app/code/Magento/Test/Block/Test.php:1" + * + * @param array|null $data An associative array containing: + * - 'file': The absolute file path (string). + * - 'line': The line number (int, optional). + * @return string A formatted string representing the relative file path and line number, + * or an empty string if the 'file' key is not present. + */ + public static function normalizeCodeLocation(?array $data): string + { + $fileName = ''; + + // Check if 'file' exists in the data array + if (isset($data['file'])) { + // Determine the position of the root path in the file path + $pos = \strpos($data['file'], self::getRootPath()); + + // If Magento root path is part of the file path, trim it to create a relative path + if (false !== $pos) { + $data['file'] = \substr( + $data['file'], + \strlen(self::getRootPath()) + 1 + ); + } + + // Format the file path and line number into the desired string format + $fileName = \sprintf('%s:%d', $data['file'], $data['line'] ?? 0); + } + + return $fileName; + } + /** * Prints or returns a backtrace * @@ -56,10 +103,10 @@ public static function backtrace($return = false, $html = true, $withArgs = true /** * Prints or return a trace * - * @param array $trace trace array - * @param bool $return return or print - * @param bool $html output in HTML format - * @param bool $withArgs add short arguments of methods + * @param array $trace trace array + * @param bool $return return or print + * @param bool $html output in HTML format + * @param bool $withArgs add short arguments of methods * @return string|bool * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) @@ -85,6 +132,9 @@ public static function trace(array $trace, $return = false, $html = true, $withA } } + // Fix static test: 'Variable $methodName might not be defined.' + $methodName = ''; + // prepare method's name if (isset($data['class']) && isset($data['function'])) { if (isset($data['object']) && get_class($data['object']) != $data['class']) { @@ -107,15 +157,7 @@ public static function trace(array $trace, $return = false, $html = true, $withA $methodName = sprintf('%s(%s)', $data['function'], join(', ', $args)); } - if (isset($data['file'])) { - $pos = strpos($data['file'], self::getRootPath()); - if ($pos !== false) { - $data['file'] = substr($data['file'], strlen(self::getRootPath()) + 1); - } - $fileName = sprintf('%s:%d', $data['file'], $data['line']); - } else { - $fileName = false; - } + $fileName = self::normalizeCodeLocation($data); if ($fileName) { $out .= sprintf('#%d %s called at [%s]', $i, $methodName, $fileName); @@ -133,6 +175,8 @@ public static function trace(array $trace, $return = false, $html = true, $withA if ($return) { return $out; } else { + // Fix static test: 'Use of echo language construct is discouraged.' + // phpcs:ignore Magento2.Security.LanguageConstruct.DirectOutput echo $out; return true; }