From 1b70e6e5e1059ab2619cec1667b115a704ba3b99 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Thu, 18 Jul 2024 11:54:58 +0300 Subject: [PATCH 1/8] Show error code --- templates/development.php | 1 + 1 file changed, 1 insertion(+) diff --git a/templates/development.php b/templates/development.php index c6a48c2..669dc1e 100644 --- a/templates/development.php +++ b/templates/development.php @@ -73,6 +73,7 @@ + (Code #getCode() ?>)
From 017ad673cafa6f22acf8d052a927dff5f5ed90f3 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Thu, 18 Jul 2024 11:55:08 +0300 Subject: [PATCH 2/8] Shift error handler itself --- src/ErrorHandler.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ErrorHandler.php b/src/ErrorHandler.php index 3e7313c..5133932 100644 --- a/src/ErrorHandler.php +++ b/src/ErrorHandler.php @@ -128,7 +128,8 @@ public function register(): void return true; } - $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); + $backtrace = debug_backtrace(); + array_shift($backtrace); throw new ErrorException($message, $severity, $severity, $file, $line, null, $backtrace); }); From b5e3c6093ec531a702685d6fa8f74e12a30babd4 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Thu, 18 Jul 2024 11:55:44 +0300 Subject: [PATCH 3/8] Fetch functions parameters --- src/Renderer/HtmlRenderer.php | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/Renderer/HtmlRenderer.php b/src/Renderer/HtmlRenderer.php index d01b87b..aa32492 100644 --- a/src/Renderer/HtmlRenderer.php +++ b/src/Renderer/HtmlRenderer.php @@ -256,22 +256,31 @@ public function renderCallStack(Throwable $t, array $trace = []): string [], ); + $index = 1; + if ($t instanceof ErrorException) { + $index = 0; + } + $length = count($trace); - for ($i = 0; $i < $length; ++$i) { - $file = !empty($trace[$i]['file']) ? $trace[$i]['file'] : null; - $line = !empty($trace[$i]['line']) ? $trace[$i]['line'] : null; - $class = !empty($trace[$i]['class']) ? $trace[$i]['class'] : null; - $args = !empty($trace[$i]['args']) ? $trace[$i]['args'] : []; + foreach ($trace as $traceItem) { + $file = !empty($traceItem['file']) ? $traceItem['file'] : null; + $line = !empty($traceItem['line']) ? $traceItem['line'] : null; + $class = !empty($traceItem['class']) ? $traceItem['class'] : null; + $args = !empty($traceItem['args']) ? $traceItem['args'] : []; $parameters = []; $function = null; - if (!empty($trace[$i]['function']) && $trace[$i]['function'] !== 'unknown') { - $function = $trace[$i]['function']; - if ($class !== null && !str_contains($function, '{closure}')) { - $parameters = (new \ReflectionMethod($class, $function))->getParameters(); + if (!empty($traceItem['function']) && $traceItem['function'] !== 'unknown') { + $function = $traceItem['function']; + if (!str_contains($function, '{closure}')) { + if ($class !== null) { + $parameters = (new \ReflectionMethod($class, $function))->getParameters(); + } else { + $parameters = (new \ReflectionFunction($function))->getParameters(); + } } } - $index = $i + 2; + $index++; if ($this->isVendorFile($file)) { $vendor[$index] = $this->renderCallStackItem( From cdbadacfbe4b31189c8a2655d6f2e6c11bba3b54 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Thu, 18 Jul 2024 12:12:15 +0300 Subject: [PATCH 4/8] Catch reflection errors --- src/Renderer/HtmlRenderer.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Renderer/HtmlRenderer.php b/src/Renderer/HtmlRenderer.php index aa32492..c0d57c8 100644 --- a/src/Renderer/HtmlRenderer.php +++ b/src/Renderer/HtmlRenderer.php @@ -273,10 +273,14 @@ public function renderCallStack(Throwable $t, array $trace = []): string if (!empty($traceItem['function']) && $traceItem['function'] !== 'unknown') { $function = $traceItem['function']; if (!str_contains($function, '{closure}')) { - if ($class !== null) { - $parameters = (new \ReflectionMethod($class, $function))->getParameters(); - } else { - $parameters = (new \ReflectionFunction($function))->getParameters(); + try { + if ($class !== null && class_exists($class)) { + $parameters = (new \ReflectionMethod($class, $function))->getParameters(); + } elseif (function_exists($function)) { + $parameters = (new \ReflectionFunction($function))->getParameters(); + } + } catch (\ReflectionException $e) { + // pass } } } From 77764e7e1b1037c8c0c8bcab2ebf266ce59b5722 Mon Sep 17 00:00:00 2001 From: xepozz Date: Thu, 18 Jul 2024 09:13:16 +0000 Subject: [PATCH 5/8] Apply Rector changes (CI) --- src/Renderer/HtmlRenderer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Renderer/HtmlRenderer.php b/src/Renderer/HtmlRenderer.php index c0d57c8..072d51c 100644 --- a/src/Renderer/HtmlRenderer.php +++ b/src/Renderer/HtmlRenderer.php @@ -279,7 +279,7 @@ public function renderCallStack(Throwable $t, array $trace = []): string } elseif (function_exists($function)) { $parameters = (new \ReflectionFunction($function))->getParameters(); } - } catch (\ReflectionException $e) { + } catch (\ReflectionException) { // pass } } From 0ab803456c87fd8f197afe67cc57e2891ae9ab2e Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Mon, 9 Sep 2024 01:45:30 +0300 Subject: [PATCH 6/8] A few cosmetic changes --- src/Renderer/HtmlRenderer.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Renderer/HtmlRenderer.php b/src/Renderer/HtmlRenderer.php index 072d51c..f51b561 100644 --- a/src/Renderer/HtmlRenderer.php +++ b/src/Renderer/HtmlRenderer.php @@ -261,7 +261,6 @@ public function renderCallStack(Throwable $t, array $trace = []): string $index = 0; } - $length = count($trace); foreach ($trace as $traceItem) { $file = !empty($traceItem['file']) ? $traceItem['file'] : null; $line = !empty($traceItem['line']) ? $traceItem['line'] : null; @@ -601,7 +600,7 @@ private function groupVendorCallStackItems(array $items): array $groupedItems[$groupIndex][$index] = $item; } - /** @psalm-var array> $groupedItems It's need for Psalm <=4.30 only. */ + /** @psalm-var array> $groupedItems It's needed for Psalm <=4.30 only. */ return $groupedItems; } From 852c3115e0b17f1c3b6960d0ff5ec9c494f47640 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Mon, 14 Oct 2024 19:43:54 +0300 Subject: [PATCH 7/8] add changelog --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 381bf08..cd0f1d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,8 @@ # Yii Error Handler Change Log -## 3.3.1 under development +## 3.4.0 under development -- no changes in this release. +- Enh #125: Add error code & show function arguments (@xepozz) ## 3.3.0 July 11, 2024 From a5b892bcfdd4a01bdcc4f33f09c528c681849a84 Mon Sep 17 00:00:00 2001 From: Dmitriy Derepko Date: Sat, 21 Dec 2024 16:39:55 +0300 Subject: [PATCH 8/8] Update src/ErrorHandler.php Co-authored-by: Sergei Predvoditelev --- src/ErrorHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ErrorHandler.php b/src/ErrorHandler.php index 79ec64d..fbf159d 100644 --- a/src/ErrorHandler.php +++ b/src/ErrorHandler.php @@ -134,7 +134,7 @@ public function register(): void return true; } - $backtrace = debug_backtrace(); + $backtrace = debug_backtrace(0); array_shift($backtrace); throw new ErrorException($message, $severity, $severity, $file, $line, null, $backtrace); });