Skip to content

Commit

Permalink
Correct handling of E_USER_ERROR as fatal error if registerErrorHandl… (
Browse files Browse the repository at this point in the history
#1670)

* Correct handling of E_USER_ERROR as fatal error if registerErrorHandler is called with callPrevious, fixes #1669
  • Loading branch information
WoZ authored Jun 9, 2022
1 parent 9c1566a commit 64854f0
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/Monolog/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class ErrorHandler
private $fatalLevel = LogLevel::ALERT;
/** @var ?string */
private $reservedMemory = null;
/** @var ?mixed */
private $lastFatalTrace;
/** @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];

Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit 64854f0

Please sign in to comment.