From 5b621dcfd43471480e774530009f900630aa7696 Mon Sep 17 00:00:00 2001 From: Dmitry Menshikov Date: Wed, 11 May 2022 20:16:47 +0300 Subject: [PATCH 1/2] Correct handling of E_USER_ERROR as fatal error if registerErrorHandler is called with callPrevious, fixes #1669 --- src/Monolog/ErrorHandler.php | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/Monolog/ErrorHandler.php b/src/Monolog/ErrorHandler.php index 16794be65..67ed64348 100644 --- a/src/Monolog/ErrorHandler.php +++ b/src/Monolog/ErrorHandler.php @@ -46,8 +46,8 @@ class ErrorHandler private $fatalLevel = LogLevel::ALERT; /** @var ?string */ private $reservedMemory = null; - /** @var ?mixed */ - private $lastFatalTrace; + /** @var ?array */ + private $lastFatalData = null; /** @var int[] */ private static $fatalErrors = [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR]; @@ -223,7 +223,7 @@ public function handleError(int $code, string $message, string $file = '', int $ } else { $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); array_shift($trace); // Exclude handleError from trace - $this->lastFatalTrace = $trace; + $this->lastFatalData = ['type' => $code, 'message' => $message, 'file' => $file, 'line' => $line, 'trace' => $trace]; } if ($this->previousErrorHandler === true) { @@ -242,12 +242,18 @@ public function handleFatalError(): void { $this->reservedMemory = ''; - $lastError = error_get_last(); + if (is_array($this->lastFatalData)) { + $lastError = $this->lastFatalData; + } else { + $lastError = error_get_last(); + } + if ($lastError && in_array($lastError['type'], self::$fatalErrors, true)) { + $trace = $lastError['trace'] ?? null; $this->logger->log( $this->fatalLevel, 'Fatal Error ('.self::codeToString($lastError['type']).'): '.$lastError['message'], - ['code' => $lastError['type'], 'message' => $lastError['message'], 'file' => $lastError['file'], 'line' => $lastError['line'], 'trace' => $this->lastFatalTrace] + ['code' => $lastError['type'], 'message' => $lastError['message'], 'file' => $lastError['file'], 'line' => $lastError['line'], 'trace' => $trace] ); if ($this->logger instanceof Logger) { From bf0c996c168de05f57ea56f5aa9920ee470cb5f0 Mon Sep 17 00:00:00 2001 From: Dmitry Menshikov Date: Thu, 9 Jun 2022 07:33:19 +0300 Subject: [PATCH 2/2] Correct handling of E_USER_ERROR as fatal error Correct handling of E_USER_ERROR as fatal error if registerErrorHandler is called with callPrevious, fixes #1669 Co-authored-by: Jordi Boggiano --- src/Monolog/ErrorHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Monolog/ErrorHandler.php b/src/Monolog/ErrorHandler.php index 67ed64348..576f1713f 100644 --- a/src/Monolog/ErrorHandler.php +++ b/src/Monolog/ErrorHandler.php @@ -46,7 +46,7 @@ class ErrorHandler private $fatalLevel = LogLevel::ALERT; /** @var ?string */ private $reservedMemory = null; - /** @var ?array */ + /** @var ?array{type: int, message: string, file: string, line: int, trace: mixed} */ private $lastFatalData = null; /** @var int[] */ private static $fatalErrors = [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR];