diff --git a/CHANGELOG.md b/CHANGELOG.md index 098b765..ec03d3e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## 4.3.2 under development -- Bug #160: Fix skipping the first trace item when handling a PHP error (@vjik) +- Bug #160, #161: Fix skipping the first trace item when handling a PHP error (@vjik) ## 4.3.1 December 18, 2025 diff --git a/src/ErrorHandler.php b/src/ErrorHandler.php index dc26b09..b201196 100644 --- a/src/ErrorHandler.php +++ b/src/ErrorHandler.php @@ -135,7 +135,12 @@ public function register(): void } $backtrace = debug_backtrace(0); - unset($backtrace[0]['function'], $backtrace[0]['class'], $backtrace[0]['type'], $backtrace[0]['args']); + if (isset($backtrace[0]['file'])) { + unset($backtrace[0]['function'], $backtrace[0]['class'], $backtrace[0]['type'], $backtrace[0]['args']); + } else { + array_shift($backtrace); + } + throw new ErrorException($message, $severity, $severity, $file, $line, null, $backtrace); }); diff --git a/tests/ErrorHandlerTest.php b/tests/ErrorHandlerTest.php index 0891104..0d1ec79 100644 --- a/tests/ErrorHandlerTest.php +++ b/tests/ErrorHandlerTest.php @@ -119,4 +119,28 @@ public function testHandleErrorWithCatching(): void $this->errorHandler->unregister(); } + + #[WithoutErrorHandler] + public function testHandleTriggerErrorWithCatching(): void + { + $this->errorHandler->register(); + $array = ['type' => 'undefined']; + + $exception = null; + try { + trigger_error('test-error'); + } catch (Throwable $exception) { + } + + $this->assertInstanceOf(ErrorException::class, $exception); + $this->assertFalse($exception::isFatalError($array)); + $this->assertNull($exception->getSolution()); + + $backtrace = $exception->getBacktrace(); + $this->assertNotEmpty($backtrace); + $this->assertArrayHasKey('file', $backtrace[0]); + $this->assertSame(__FILE__, $backtrace[0]['file']); + + $this->errorHandler->unregister(); + } }