W3cubDocs

/Symfony 2.7

Symfony\Component\Debug\ErrorHandler

class ErrorHandler

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, only once for repeated errors - 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 EERROR. EDEPRECATED and EUSERDEPRECATED levels never throw. ERECOVERABLEERROR and EUSERERROR 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.

Constants

TYPE_DEPRECATION

Methods

static ErrorHandler register(ErrorHandler|null|int $handler = null, bool $replace = true)

Registers the error handler.

setDefaultLogger(LoggerInterface $logger, array|int $levels = null, bool $replace = false)

Sets a logger to non assigned errors levels.

array setLoggers(array $loggers)

Sets a logger for each error level.

callable|null setExceptionHandler(callable $handler)

Sets a user exception handler.

int throwAt(int $levels, bool $replace = false)

Sets the error levels that are to be thrown.

int scopeAt(int $levels, bool $replace = false)

Sets the error levels that are logged or thrown with their local scope.

int traceAt(int $levels, bool $replace = false)

Sets the error levels that are logged with their stack trace.

int screamAt(int $levels, bool $replace = false)

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

bool handleError($type, $message, $file, $line, array $context)

Handles errors by filtering then logging them according to the configured bit fields.

handleException(Exception $exception, array $error = null)

Handles an exception by logging then forwarding it to an other handler.

static handleFatalError(array $error = null)

Shutdown registered function for handling PHP fatal errors.

static stackErrors()

Configures the error handler for delayed handling.

static unstackErrors()

Unstacks stacked errors and forwards to the logger

setLevel(int|null $level)

Sets the level at which the conversion to Exception is done.

setDisplayErrors(int $displayErrors)

Sets the display_errors flag value.

static setLogger(LoggerInterface $logger, string $channel = 'deprecation')

Sets a logger for the given channel.

handle($level, $message, $file = 'unknown', $line, $context = array())
handleFatal()

Handles PHP fatal errors.

Details

static public ErrorHandler register(ErrorHandler|null|int $handler = null, bool $replace = true)

Registers the error handler.

Parameters

ErrorHandler|null|int $handler The handler to register, or @deprecated (since 2.6, to be removed in 3.0) bit field of thrown levels
bool $replace Whether to replace or not any existing handler

Return Value

ErrorHandler The registered error handler

public setDefaultLogger(LoggerInterface $logger, array|int $levels = null, bool $replace = false)

Sets a logger to non assigned errors levels.

Parameters

LoggerInterface $logger A PSR-3 logger to put as default for the given levels
array|int $levels An array map of E* to LogLevel::* or an integer bit field of E* constants
bool $replace Whether to replace or not any existing logger

public array setLoggers(array $loggers)

Sets a logger for each error level.

Parameters

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

Return Value

array The previous map

Exceptions

InvalidArgumentException

public callable|null setExceptionHandler(callable $handler)

Sets a user exception handler.

Parameters

callable $handler A handler that will be called on Exception

Return Value

callable|null The previous exception handler

Exceptions

InvalidArgumentException

public int throwAt(int $levels, bool $replace = false)

Sets the error levels that are to be thrown.

Parameters

int $levels A bit field of E_* constants for thrown errors
bool $replace Replace or amend the previous value

Return Value

int The previous value

public int scopeAt(int $levels, bool $replace = false)

Sets the error levels that are logged or thrown with their local scope.

Parameters

int $levels A bit field of E_* constants for scoped errors
bool $replace Replace or amend the previous value

Return Value

int The previous value

public int traceAt(int $levels, bool $replace = false)

Sets the error levels that are logged with their stack trace.

Parameters

int $levels A bit field of E_* constants for traced errors
bool $replace Replace or amend the previous value

Return Value

int The previous value

public int screamAt(int $levels, bool $replace = false)

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

Parameters

int $levels A bit field of E_* constants for screamed errors
bool $replace Replace or amend the previous value

Return Value

int The previous value

public bool handleError($type, $message, $file, $line, array $context)

Handles errors by filtering then logging them according to the configured bit fields.

Parameters

$type
$message
$file
$line
array $context

Return Value

bool Returns false when no handling happens so that the PHP engine can handle the error itself.

Exceptions

ErrorException When $this->thrownErrors requests so

public handleException(Exception $exception, array $error = null)

Handles an exception by logging then forwarding it to an other handler.

Parameters

Exception $exception An exception to handle
array $error An array as returned by errorgetlast()

static public handleFatalError(array $error = null)

Shutdown registered function for handling PHP fatal errors.

Parameters

array $error An array as returned by errorgetlast()

static public stackErrors()

Configures the error handler for delayed handling.

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.

static public unstackErrors()

Unstacks stacked errors and forwards to the logger

public setLevel(int|null $level)

Sets the level at which the conversion to Exception is done.

Parameters

int|null $level The level (null to use the error_reporting() value and 0 to disable)

public setDisplayErrors(int $displayErrors)

Sets the display_errors flag value.

Parameters

int $displayErrors The display_errors flag value

static public setLogger(LoggerInterface $logger, string $channel = 'deprecation')

Sets a logger for the given channel.

Parameters

LoggerInterface $logger A logger interface
string $channel The channel associated with the logger (deprecation, emergency or scream)

public handle($level, $message, $file = 'unknown', $line, $context = array())

Parameters

$level
$message
$file
$line
$context

public handleFatal()

Handles PHP fatal errors.

© 2004–2016 Fabien Potencier
Licensed under the MIT License.
http://api.symfony.com/2.7/Symfony/Component/Debug/ErrorHandler.html