ErrorHandler
API reference › yii › base › ErrorHandler
2 subclasses
ErrorHandler handles uncaught PHP errors and exceptions.
ErrorHandler is configured as an application component in Application by default.
You can access that instance via Yii::$app->errorHandler.
For more details and usage information on ErrorHandler, see the guide article on handling errors.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$behaviors read-only | array<Behavior<static>> | List of behaviors attached to this component. | Component |
$discardExistingOutput | bool | Whether to discard any existing page output before error display. Default: true. | ErrorHandler |
$exception | Throwable| | The exception that is being handled currently. Default: null. | ErrorHandler |
$memoryReserveSize | int | The size of the reserved memory. Default: 262144. | ErrorHandler |
$silentExitOnException | bool | If true - handleException() will finish script with ExitCode::OK. false - ExitCode::UNSPECIFIED_ERROR. Default: null. Available since 2.0.36. | ErrorHandler |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | BaseObject |
__call() | Calls the named method which is not a class method. | Component |
__clone() | This method is called after the object is created by cloning an existing one. | Component |
__get() | Returns the value of a component property. | Component |
__isset() | Checks if a property is set, i.e. defined and not null. | Component |
__set() | Sets the value of a component property. | Component |
__unset() | Sets a component property to be null. | Component |
attachBehavior() | Attaches a behavior to this component. | Component |
attachBehaviors() | Attaches a list of behaviors to the component. | Component |
behaviors() | Returns a list of behaviors that this component should behave as. | Component |
canGetProperty() | Returns a value indicating whether a property can be read. | Component |
canSetProperty() | Returns a value indicating whether a property can be set. | Component |
className() static | Returns the fully qualified name of this class. | BaseObject |
clearOutput() | Removes all output echoed before calling this method. | ErrorHandler |
convertExceptionToString() static | Converts an exception into a simple string. | ErrorHandler |
convertExceptionToVerboseString() static | Converts an exception into a string that has verbose information about the exception and its trace. | ErrorHandler |
detachBehavior() | Detaches a behavior from the component. | Component |
detachBehaviors() | Detaches all behaviors from the component. | Component |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | Component |
getBehavior() | Returns the named behavior object. | Component |
getBehaviors() | Returns all behaviors attached to this component. | Component |
handleError() | Handles PHP execution errors such as warnings and notices. | ErrorHandler |
handleException() | Handles uncaught PHP exceptions. | ErrorHandler |
handleFatalError() | Handles fatal PHP errors. | ErrorHandler |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | Component |
hasMethod() | Returns a value indicating whether a method is defined. | Component |
hasProperty() | Returns a value indicating whether a property is defined for this component. | Component |
init() | ErrorHandler | |
logException() | Logs the given exception. | ErrorHandler |
off() | Detaches an existing event handler from this component. | Component |
on() | Attaches an event handler to an event. | Component |
register() | Register this error handler. | ErrorHandler |
trigger() | Triggers an event. | Component |
unregister() | Unregisters this error handler by restoring the PHP error and exception handlers. | ErrorHandler |
handleFallbackExceptionMessage() protected | Handles exception thrown during exception processing in handleException(). | ErrorHandler |
renderException() protected abstract | Renders the exception. | ErrorHandler |
Events
Section titled “Events”| Event | Type | Description | Defined by |
|---|---|---|---|
EVENT_SHUTDOWN | Event | An event that is triggered when the handler is called by shutdown function via handleFatalError(). Event name: 'shutdown'. Available since 2.0.46. | ErrorHandler |
Property details
Section titled “Property details”$discardExistingOutput
Section titled “$discardExistingOutput ”public bool $discardExistingOutput = trueWhether to discard any existing page output before error display.
Defaults to true.
$memoryReserveSize
Section titled “$memoryReserveSize ”public int $memoryReserveSize = 262144The size of the reserved memory.
A portion of memory is pre-allocated so that when an out-of-memory issue occurs, the error handler is able to handle the error with the help of this reserved memory. If you set this value to be 0, no memory will be reserved. Defaults to 256KB.
Method details
Section titled “Method details”clearOutput()
Section titled “clearOutput() ”public function clearOutput(): mixedRemoves all output echoed before calling this method.
Returns
mixed
convertExceptionToString()
Section titled “convertExceptionToString() ”public static function convertExceptionToString(Throwable $exception): stringConverts an exception into a simple string.
Parameters
| Parameter | Type | Description |
|---|---|---|
$exception | Throwable | The exception being converted |
Returns
string: The string representation of the exception.
convertExceptionToVerboseString()
Section titled “convertExceptionToVerboseString() ”public static function convertExceptionToVerboseString(Throwable $exception): stringConverts an exception into a string that has verbose information about the exception and its trace.
Parameters
| Parameter | Type | Description |
|---|---|---|
$exception | Throwable | The exception being converted |
Returns
string: The string representation of the exception.
Since
2.0.14
handleError()
Section titled “handleError() ”public function handleError(int $code, string $message, string $file, int $line): boolHandles PHP execution errors such as warnings and notices.
This method is used as a PHP error handler. It will simply raise an ErrorException.
Parameters
| Parameter | Type | Description |
|---|---|---|
$code | int | The level of the error raised. |
$message | string | The error message. |
$file | string | The filename that the error was raised in. |
$line | int | The line number the error was raised at. |
Returns
bool: Whether the normal error handler continues.
Throws
handleException()
Section titled “handleException() ”public function handleException(Throwable $exception): mixedHandles uncaught PHP exceptions.
This method is implemented as a PHP exception handler.
Parameters
| Parameter | Type | Description |
|---|---|---|
$exception | Throwable | The exception that is not caught |
Returns
mixed
handleFatalError()
Section titled “handleFatalError() ”public function handleFatalError(): mixedHandles fatal PHP errors.
Returns
mixed
logException()
Section titled “logException() ”public function logException(Throwable $exception): mixedLogs the given exception.
register()
Section titled “register() ”public function register(): mixedRegister this error handler.
Returns
mixed
unregister()
Section titled “unregister() ”public function unregister(): mixedUnregisters this error handler by restoring the PHP error and exception handlers.
Returns
mixed
handleFallbackExceptionMessage()
Section titled “handleFallbackExceptionMessage() ”protected function handleFallbackExceptionMessage(Throwable $exception, Throwable $previousException): mixedHandles exception thrown during exception processing in handleException().
Parameters
| Parameter | Type | Description |
|---|---|---|
$exception | Throwable | Exception that was thrown during main exception processing. |
$previousException | Throwable | Main exception processed in handleException(). |
Returns
mixed
Since
2.0.11
renderException()
Section titled “renderException() ”abstract protected function renderException(Throwable $exception): mixedRenders the exception.