Documentation

ErrorHandler
in package

A generic ErrorHandler for the PHP engine.

Provides five bit fields that control how errors are handled:

  • thrownErrors: errors thrown as \ErrorException
  • loggedErrors: logged errors, when not @-silenced
  • scopedErrors: errors thrown or logged with their local context
  • tracedErrors: errors logged with their stack trace
  • screamedErrors: never @-silenced errors

Each error level can be logged by a dedicated PSR-3 logger object. Screaming only applies to logging. Throwing takes precedence over logging. Uncaught exceptions are logged as E_ERROR. E_DEPRECATED and E_USER_DEPRECATED levels never throw. E_RECOVERABLE_ERROR and E_USER_ERROR levels always throw. Non catchable errors that can be detected at shutdown time are logged when the scream bit field allows so. As errors have a performance cost, repeated errors are all logged, so that the developer can see them and weight them as more important to fix than others of the same level.

Tags
author

Nicolas Grekas p@tchwork.com

author

Grégoire Pineau lyrixx@lyrixx.info

Table of Contents

Properties

$bootstrappingLogger  : mixed
$exceptionHandler  : mixed
$exitCode  : mixed
$isRecursive  : mixed
$isRoot  : mixed
$levels  : mixed
$loggedErrors  : mixed
$loggers  : mixed
$reservedMemory  : mixed
$scopedErrors  : mixed
$screamedErrors  : mixed
$silencedErrorCache  : mixed
$silencedErrorCount  : mixed
$stackedErrorLevels  : mixed
$stackedErrors  : mixed
$thrownErrors  : mixed
$toStringException  : mixed
$tracedErrors  : mixed
$traceReflector  : mixed

Methods

__construct()  : mixed
register()  : self
Registers the error handler.
scopeAt()  : int
Sets the PHP error levels for which local variables are preserved.
screamAt()  : int
Sets the error levels where the @-operator is ignored.
setDefaultLogger()  : mixed
Sets a logger to non assigned errors levels.
setExceptionHandler()  : callable|null
Sets a user exception handler.
setLoggers()  : array<string|int, mixed>
Sets a logger for each error level.
stackErrors()  : mixed
Configures the error handler for delayed handling.
throwAt()  : int
Sets the PHP error levels that throw an exception when a PHP error occurs.
traceAt()  : int
Sets the PHP error levels for which the stack trace is preserved.
unstackErrors()  : mixed
Unstacks stacked errors and forwards to the logger.
getFatalErrorHandlers()  : array<string|int, FatalErrorHandlerInterface>
Gets the fatal error handlers.
cleanTrace()  : mixed
reRegister()  : mixed
Re-registers as a PHP error handler if levels changed.

Properties

$levels

private mixed $levels = array(E_DEPRECATED => 'Deprecated', E_USER_DEPRECATED => 'User Deprecated', E_NOTICE => 'Notice', E_USER_NOTICE => 'User Notice', E_STRICT => 'Runtime Notice', E_WARNING => 'Warning', E_USER_WARNING => 'User Warning', E_COMPILE_WARNING => 'Compile Warning', E_CORE_WARNING => 'Core Warning', E_USER_ERROR => 'User Error', E_RECOVERABLE_ERROR => 'Catchable Fatal Error', E_COMPILE_ERROR => 'Compile Error', E_PARSE => 'Parse Error', E_ERROR => 'Error', E_CORE_ERROR => 'Core Error')

$loggers

private mixed $loggers = array(E_DEPRECATED => array(null, \Psr\Log\LogLevel::INFO), E_USER_DEPRECATED => array(null, \Psr\Log\LogLevel::INFO), E_NOTICE => array(null, \Psr\Log\LogLevel::WARNING), E_USER_NOTICE => array(null, \Psr\Log\LogLevel::WARNING), E_STRICT => array(null, \Psr\Log\LogLevel::WARNING), E_WARNING => array(null, \Psr\Log\LogLevel::WARNING), E_USER_WARNING => array(null, \Psr\Log\LogLevel::WARNING), E_COMPILE_WARNING => array(null, \Psr\Log\LogLevel::WARNING), E_CORE_WARNING => array(null, \Psr\Log\LogLevel::WARNING), E_USER_ERROR => array(null, \Psr\Log\LogLevel::CRITICAL), E_RECOVERABLE_ERROR => array(null, \Psr\Log\LogLevel::CRITICAL), E_COMPILE_ERROR => array(null, \Psr\Log\LogLevel::CRITICAL), E_PARSE => array(null, \Psr\Log\LogLevel::CRITICAL), E_ERROR => array(null, \Psr\Log\LogLevel::CRITICAL), E_CORE_ERROR => array(null, \Psr\Log\LogLevel::CRITICAL))

$silencedErrorCache

private static mixed $silencedErrorCache = array()

$silencedErrorCount

private static mixed $silencedErrorCount = 0

$stackedErrorLevels

private static mixed $stackedErrorLevels = array()

$stackedErrors

private static mixed $stackedErrors = array()

$toStringException

private static mixed $toStringException = null

Methods

register()

Registers the error handler.

public static register([self|null $handler = null ][, bool $replace = true ]) : self
Parameters
$handler : self|null = null

The handler to register

$replace : bool = true

Whether to replace or not any existing handler

Return values
self

The registered error handler

scopeAt()

Sets the PHP error levels for which local variables are preserved.

public scopeAt(int $levels[, bool $replace = false ]) : int
Parameters
$levels : int

A bit field of E_* constants for scoped errors

$replace : bool = false

Replace or amend the previous value

Return values
int

The previous value

screamAt()

Sets the error levels where the @-operator is ignored.

public screamAt(int $levels[, bool $replace = false ]) : int
Parameters
$levels : int

A bit field of E_* constants for screamed errors

$replace : bool = false

Replace or amend the previous value

Return values
int

The previous value

setDefaultLogger()

Sets a logger to non assigned errors levels.

public setDefaultLogger(LoggerInterface $logger[, array<string|int, mixed>|int $levels = E_ALL ][, bool $replace = false ]) : mixed
Parameters
$logger : LoggerInterface

A PSR-3 logger to put as default for the given levels

$levels : array<string|int, mixed>|int = E_ALL

An array map of E_* to LogLevel::* or an integer bit field of E_* constants

$replace : bool = false

Whether to replace or not any existing logger

setExceptionHandler()

Sets a user exception handler.

public setExceptionHandler([callable $handler = null ]) : callable|null
Parameters
$handler : callable = null

A handler that will be called on Exception

Return values
callable|null

The previous exception handler

setLoggers()

Sets a logger for each error level.

public setLoggers(array<string|int, mixed> $loggers) : array<string|int, mixed>
Parameters
$loggers : array<string|int, mixed>

Error levels to [LoggerInterface|null, LogLevel::*] map

Tags
throws
InvalidArgumentException
Return values
array<string|int, mixed>

The previous map

stackErrors()

Configures the error handler for delayed handling.

public static stackErrors() : mixed

Ensures also that non-catchable fatal errors are never silenced.

As shown by http://bugs.php.net/42098 and http://bugs.php.net/60724 PHP has a compile stage where it behaves unusually. To workaround it, we plug an error handler that only stacks errors for later.

The most important feature of this is to prevent autoloading until unstackErrors() is called.

throwAt()

Sets the PHP error levels that throw an exception when a PHP error occurs.

public throwAt(int $levels[, bool $replace = false ]) : int
Parameters
$levels : int

A bit field of E_* constants for thrown errors

$replace : bool = false

Replace or amend the previous value

Return values
int

The previous value

traceAt()

Sets the PHP error levels for which the stack trace is preserved.

public traceAt(int $levels[, bool $replace = false ]) : int
Parameters
$levels : int

A bit field of E_* constants for traced errors

$replace : bool = false

Replace or amend the previous value

Return values
int

The previous value

unstackErrors()

Unstacks stacked errors and forwards to the logger.

public static unstackErrors() : mixed

cleanTrace()

private cleanTrace(mixed $backtrace, mixed $type, mixed $file, mixed $line, mixed $throw) : mixed
Parameters
$backtrace : mixed
$type : mixed
$file : mixed
$line : mixed
$throw : mixed

reRegister()

Re-registers as a PHP error handler if levels changed.

private reRegister(mixed $prev) : mixed
Parameters
$prev : mixed

        
On this page

Search results