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

final

Table of Contents

$bootstrappingLogger  : mixed
$configureException  : mixed
$debug  : 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
$thrownErrors  : mixed
$toStringException  : mixed
$tracedErrors  : mixed
__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
Sets a user exception handler.
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

$levels

private 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']

$loggers

private mixed $loggers = [E_DEPRECATED => [null, PsrLogLogLevel::INFO], E_USER_DEPRECATED => [null, PsrLogLogLevel::INFO], E_NOTICE => [null, PsrLogLogLevel::WARNING], E_USER_NOTICE => [null, PsrLogLogLevel::WARNING], E_STRICT => [null, PsrLogLogLevel::WARNING], E_WARNING => [null, PsrLogLogLevel::WARNING], E_USER_WARNING => [null, PsrLogLogLevel::WARNING], E_COMPILE_WARNING => [null, PsrLogLogLevel::WARNING], E_CORE_WARNING => [null, PsrLogLogLevel::WARNING], E_USER_ERROR => [null, PsrLogLogLevel::CRITICAL], E_RECOVERABLE_ERROR => [null, PsrLogLogLevel::CRITICAL], E_COMPILE_ERROR => [null, PsrLogLogLevel::CRITICAL], E_PARSE => [null, PsrLogLogLevel::CRITICAL], E_ERROR => [null, PsrLogLogLevel::CRITICAL], E_CORE_ERROR => [null, PsrLogLogLevel::CRITICAL]]

$silencedErrorCache

private static mixed $silencedErrorCache = []

$silencedErrorCount

private static mixed $silencedErrorCount = 0

$toStringException

private static mixed $toStringException

Methods

__construct()

public __construct([BufferingLogger|null $bootstrappingLogger = null ][, bool $debug = false ]) : mixed
Parameters
$bootstrappingLogger : BufferingLogger|null = null
$debug : bool = false
Return values
mixed

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
throws
ErrorException

When $function(...$arguments) triggers a PHP error

Return values
mixed

What $function(...$arguments) returns

register()

Registers the error handler.

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

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|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

Return values
void

setExceptionHandler()

Sets a user exception handler.

public setExceptionHandler(callable $handler) : callable|null
Parameters
$handler : callable
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

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

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
string

renderException()

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
Return values
void

reRegister()

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

private reRegister(int $prev) : void
Parameters
$prev : int
Return values
void

Search results