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
Table of Contents
Properties
- $bootstrappingLogger : mixed
- $configureException : Closure
- $debug : bool
- $exceptionHandler : mixed
- $exitCode : int
- $isRecursive : bool
- $isRoot : bool
- $levels : array<string|int, mixed>
- $loggedErrors : int
- $loggers : array<string|int, mixed>
- $reservedMemory : string|null
- $scopedErrors : int
- $screamedErrors : int
- $silencedErrorCache : array<string|int, mixed>
- $silencedErrorCount : int
- $thrownErrors : int
- $tracedErrors : int
Methods
- __construct() : mixed
- call() : mixed
- Calls a function and turns any PHP error into \ErrorException.
- 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() : void
- Sets a logger to non assigned errors levels.
- setExceptionHandler() : callable|null
- setLoggers() : array<string|int, mixed>
- Sets a logger for each error level.
- 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.
- getErrorEnhancers() : array<string|int, ErrorEnhancerInterface>
- Override this method if you want to define more error enhancers.
- cleanTrace() : array<string|int, mixed>
- Cleans the trace by removing function arguments and the frames added by the error handler and DebugClassLoader.
- parseAnonymousClass() : string
- Parse the error message by removing the anonymous class notation and using the parent class instead if possible.
- renderException() : void
- Renders the given exception.
- reRegister() : void
- Re-registers as a PHP error handler if levels changed.
Properties
$bootstrappingLogger
private
mixed
$bootstrappingLogger
= null
$configureException
private
Closure
$configureException
$debug
private
bool
$debug
$exceptionHandler
private
mixed
$exceptionHandler
$exitCode
private
static int
$exitCode
= 0
$isRecursive
private
bool
$isRecursive
= false
$isRoot
private
bool
$isRoot
= false
$levels
private
array<string|int, mixed>
$levels
= [\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']
$loggedErrors
private
int
$loggedErrors
= 0
$loggers
private
array<string|int, mixed>
$loggers
= [\E_DEPRECATED => [null, \Psr\Log\LogLevel::INFO], \E_USER_DEPRECATED => [null, \Psr\Log\LogLevel::INFO], \E_NOTICE => [null, \Psr\Log\LogLevel::WARNING], \E_USER_NOTICE => [null, \Psr\Log\LogLevel::WARNING], \E_STRICT => [null, \Psr\Log\LogLevel::WARNING], \E_WARNING => [null, \Psr\Log\LogLevel::WARNING], \E_USER_WARNING => [null, \Psr\Log\LogLevel::WARNING], \E_COMPILE_WARNING => [null, \Psr\Log\LogLevel::WARNING], \E_CORE_WARNING => [null, \Psr\Log\LogLevel::WARNING], \E_USER_ERROR => [null, \Psr\Log\LogLevel::CRITICAL], \E_RECOVERABLE_ERROR => [null, \Psr\Log\LogLevel::CRITICAL], \E_COMPILE_ERROR => [null, \Psr\Log\LogLevel::CRITICAL], \E_PARSE => [null, \Psr\Log\LogLevel::CRITICAL], \E_ERROR => [null, \Psr\Log\LogLevel::CRITICAL], \E_CORE_ERROR => [null, \Psr\Log\LogLevel::CRITICAL]]
$reservedMemory
private
static string|null
$reservedMemory
= null
$scopedErrors
private
int
$scopedErrors
= 0x1fff
$screamedErrors
private
int
$screamedErrors
= 0x55
$silencedErrorCache
private
static array<string|int, mixed>
$silencedErrorCache
= []
$silencedErrorCount
private
static int
$silencedErrorCount
= 0
$thrownErrors
private
int
$thrownErrors
= 0x1fff
$tracedErrors
private
int
$tracedErrors
= 0x77fb
Methods
__construct()
public
__construct([BufferingLogger $bootstrappingLogger = null ][, bool $debug = false ]) : mixed
Parameters
- $bootstrappingLogger : BufferingLogger = null
- $debug : bool = false
call()
Calls a function and turns any PHP error into \ErrorException.
public
static call(callable $function, mixed ...$arguments) : mixed
Parameters
- $function : callable
- $arguments : mixed
Tags
register()
Registers the error handler.
public
static register([self $handler = null ][, bool $replace = true ]) : self
Parameters
- $handler : self = null
- $replace : bool = true
Return values
selfscopeAt()
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
intscreamAt()
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
intsetDefaultLogger()
Sets a logger to non assigned errors levels.
public
setDefaultLogger(LoggerInterface $logger[, array<string|int, mixed>|int|null $levels = E_ALL ][, bool $replace = false ]) : void
Parameters
- $logger : LoggerInterface
-
A PSR-3 logger to put as default for the given levels
- $levels : array<string|int, mixed>|int|null = 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()
public
setExceptionHandler(callable|null $handler) : callable|null
Parameters
- $handler : callable|null
Return values
callable|nullsetLoggers()
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
Return values
array<string|int, mixed>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
inttraceAt()
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
intgetErrorEnhancers()
Override this method if you want to define more error enhancers.
protected
getErrorEnhancers() : array<string|int, ErrorEnhancerInterface>
Return values
array<string|int, ErrorEnhancerInterface>cleanTrace()
Cleans the trace by removing function arguments and the frames added by the error handler and DebugClassLoader.
private
cleanTrace(array<string|int, mixed> $backtrace, int $type, string &$file, int &$line, bool $throw) : array<string|int, mixed>
Parameters
- $backtrace : array<string|int, mixed>
- $type : int
- $file : string
- $line : int
- $throw : bool
Return values
array<string|int, mixed>parseAnonymousClass()
Parse the error message by removing the anonymous class notation and using the parent class instead if possible.
private
parseAnonymousClass(string $message) : string
Parameters
- $message : string
Return values
stringrenderException()
Renders the given exception.
private
renderException(Throwable $exception) : void
As this method is mainly called during boot where nothing is yet available, the output is always either HTML or CLI depending where PHP runs.
Parameters
- $exception : Throwable
reRegister()
Re-registers as a PHP error handler if levels changed.
private
reRegister(int $prev) : void
Parameters
- $prev : int