Skip to content

Commit 16a53fd

Browse files
xepozzvjik
andauthored
Add error code & show function arguments (#125)
Co-authored-by: Sergei Predvoditelev <sergei@predvoditelev.ru>
1 parent 46b059d commit 16a53fd

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# Yii Error Handler Change Log
22

3-
## 3.3.1 under development
3+
## 3.4.0 under development
44

5+
- Enh #125: Add error code & show function arguments (@xepozz)
56
- Enh #130: Pass exception message instead of rendered exception to logger in `ErrorHandler` (@olegbaturin)
67
- Enh #133: Extract response generator from `ErrorCatcher` middleware into separate `ThrowableResponseFactory` class (@olegbaturin)
78

src/ErrorHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ public function register(): void
134134
return true;
135135
}
136136

137-
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
137+
$backtrace = debug_backtrace(0);
138+
array_shift($backtrace);
138139
throw new ErrorException($message, $severity, $severity, $file, $line, null, $backtrace);
139140
});
140141

src/Renderer/HtmlRenderer.php

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -256,22 +256,34 @@ public function renderCallStack(Throwable $t, array $trace = []): string
256256
[],
257257
);
258258

259-
$length = count($trace);
260-
for ($i = 0; $i < $length; ++$i) {
261-
$file = !empty($trace[$i]['file']) ? $trace[$i]['file'] : null;
262-
$line = !empty($trace[$i]['line']) ? $trace[$i]['line'] : null;
263-
$class = !empty($trace[$i]['class']) ? $trace[$i]['class'] : null;
264-
$args = !empty($trace[$i]['args']) ? $trace[$i]['args'] : [];
259+
$index = 1;
260+
if ($t instanceof ErrorException) {
261+
$index = 0;
262+
}
263+
264+
foreach ($trace as $traceItem) {
265+
$file = !empty($traceItem['file']) ? $traceItem['file'] : null;
266+
$line = !empty($traceItem['line']) ? $traceItem['line'] : null;
267+
$class = !empty($traceItem['class']) ? $traceItem['class'] : null;
268+
$args = !empty($traceItem['args']) ? $traceItem['args'] : [];
265269

266270
$parameters = [];
267271
$function = null;
268-
if (!empty($trace[$i]['function']) && $trace[$i]['function'] !== 'unknown') {
269-
$function = $trace[$i]['function'];
270-
if ($class !== null && !str_contains($function, '{closure}')) {
271-
$parameters = (new \ReflectionMethod($class, $function))->getParameters();
272+
if (!empty($traceItem['function']) && $traceItem['function'] !== 'unknown') {
273+
$function = $traceItem['function'];
274+
if (!str_contains($function, '{closure}')) {
275+
try {
276+
if ($class !== null && class_exists($class)) {
277+
$parameters = (new \ReflectionMethod($class, $function))->getParameters();
278+
} elseif (function_exists($function)) {
279+
$parameters = (new \ReflectionFunction($function))->getParameters();
280+
}
281+
} catch (\ReflectionException) {
282+
// pass
283+
}
272284
}
273285
}
274-
$index = $i + 2;
286+
$index++;
275287

276288
if ($this->isVendorFile($file)) {
277289
$vendor[$index] = $this->renderCallStackItem(
@@ -588,7 +600,7 @@ private function groupVendorCallStackItems(array $items): array
588600
$groupedItems[$groupIndex][$index] = $item;
589601
}
590602

591-
/** @psalm-var array<int, array<int, string>> $groupedItems It's need for Psalm <=4.30 only. */
603+
/** @psalm-var array<int, array<int, string>> $groupedItems It's needed for Psalm <=4.30 only. */
592604

593605
return $groupedItems;
594606
}

templates/development.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
<?php else: ?>
7474
<span><?= $exceptionClass ?></span>
7575
<?php endif ?>
76+
(Code #<?= $throwable->getCode() ?>)
7677
</div>
7778

7879
<div class="exception-message">

0 commit comments

Comments
 (0)