Skip to content

Commit a7db1f3

Browse files
committed
Fix empty first trace item when handling trigger_error
1 parent 3418351 commit a7db1f3

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 4.3.2 under development
44

5-
- Bug #160: Fix skipping the first trace item when handling a PHP error (@vjik)
5+
- Bug #160, #161: Fix skipping the first trace item when handling a PHP error (@vjik)
66

77
## 4.3.1 December 18, 2025
88

src/ErrorHandler.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,12 @@ public function register(): void
135135
}
136136

137137
$backtrace = debug_backtrace(0);
138-
unset($backtrace[0]['function'], $backtrace[0]['class'], $backtrace[0]['type'], $backtrace[0]['args']);
138+
if (isset($backtrace[0]['file'])) {
139+
unset($backtrace[0]['function'], $backtrace[0]['class'], $backtrace[0]['type'], $backtrace[0]['args']);
140+
} else {
141+
array_shift($backtrace);
142+
}
143+
139144
throw new ErrorException($message, $severity, $severity, $file, $line, null, $backtrace);
140145
});
141146

tests/ErrorHandlerTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,28 @@ public function testHandleErrorWithCatching(): void
119119

120120
$this->errorHandler->unregister();
121121
}
122+
123+
#[WithoutErrorHandler]
124+
public function testHandleTriggerErrorWithCatching(): void
125+
{
126+
$this->errorHandler->register();
127+
$array = ['type' => 'undefined'];
128+
129+
$exception = null;
130+
try {
131+
trigger_error('test-error');
132+
} catch (Throwable $exception) {
133+
}
134+
135+
$this->assertInstanceOf(ErrorException::class, $exception);
136+
$this->assertFalse($exception::isFatalError($array));
137+
$this->assertNull($exception->getSolution());
138+
139+
$backtrace = $exception->getBacktrace();
140+
$this->assertNotEmpty($backtrace);
141+
$this->assertArrayHasKey('file', $backtrace[0]);
142+
$this->assertSame(__FILE__, $backtrace[0]['file']);
143+
144+
$this->errorHandler->unregister();
145+
}
122146
}

0 commit comments

Comments
 (0)