1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-05 12:47:39 +02:00

Correct handling of E_USER_ERROR as fatal error if registerErrorHandl… (#1670)

* Correct handling of E_USER_ERROR as fatal error if registerErrorHandler is called with callPrevious, fixes #1669
This commit is contained in:
Dmitry Menshikov
2022-06-09 10:04:15 +03:00
committed by GitHub
parent 9c1566a971
commit 64854f09da

View File

@@ -46,8 +46,8 @@ class ErrorHandler
private $fatalLevel = LogLevel::ALERT; private $fatalLevel = LogLevel::ALERT;
/** @var ?string */ /** @var ?string */
private $reservedMemory = null; private $reservedMemory = null;
/** @var ?mixed */ /** @var ?array{type: int, message: string, file: string, line: int, trace: mixed} */
private $lastFatalTrace; private $lastFatalData = null;
/** @var int[] */ /** @var int[] */
private static $fatalErrors = [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR]; private static $fatalErrors = [E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR];
@@ -223,7 +223,7 @@ class ErrorHandler
} else { } else {
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
array_shift($trace); // Exclude handleError from trace 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) { if ($this->previousErrorHandler === true) {
@@ -242,12 +242,18 @@ class ErrorHandler
{ {
$this->reservedMemory = ''; $this->reservedMemory = '';
if (is_array($this->lastFatalData)) {
$lastError = $this->lastFatalData;
} else {
$lastError = error_get_last(); $lastError = error_get_last();
}
if ($lastError && in_array($lastError['type'], self::$fatalErrors, true)) { if ($lastError && in_array($lastError['type'], self::$fatalErrors, true)) {
$trace = $lastError['trace'] ?? null;
$this->logger->log( $this->logger->log(
$this->fatalLevel, $this->fatalLevel,
'Fatal Error ('.self::codeToString($lastError['type']).'): '.$lastError['message'], '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) { if ($this->logger instanceof Logger) {