ErrorHandler
API reference › yii › web › ErrorHandler
1 subclass
ErrorHandler handles uncaught PHP errors and exceptions.
ErrorHandler displays these errors using appropriate views based on the nature of the errors and the mode the application runs at.
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<yii\<static>> | List of behaviors attached to this component. | yii\ |
$callStackItemView | string | The path of the view file for rendering exceptions and errors call stack element. Default: '@yii/views/errorHandler/callStackItem.php'. | ErrorHandler |
$discardExistingOutput | bool | Whether to discard any existing page output before error display. Default: true. | yii\ |
$displayVars | array | List of the PHP predefined variables that should be displayed on the error page. Default: ['_GET',. Available since 2.0.7. | ErrorHandler |
$errorAction | string| | The route (e.g. site/error) to the controller action that will be used to display external errors. Default: null. | ErrorHandler |
$errorView | string | The path of the view file for rendering exceptions without call stack information. Default: '@yii/views/errorHandler/error.php'. | ErrorHandler |
$exception | Throwable| | The exception that is being handled currently. Default: null. | yii\ |
$exceptionView | string | The path of the view file for rendering exceptions. Default: '@yii/views/errorHandler/exception.php'. | ErrorHandler |
$maxSourceLines | int | Maximum number of source code lines to be displayed. Default: 19. | ErrorHandler |
$maxTraceSourceLines | int | Maximum number of trace source code lines to be displayed. Default: 13. | ErrorHandler |
$memoryReserveSize | int | The size of the reserved memory. Default: 262144. | yii\ |
$previousExceptionView | string | The path of the view file for rendering previous exceptions. Default: '@yii/views/errorHandler/previousException.php'. | ErrorHandler |
$silentExitOnException | bool | If true - handleException() will finish script with ExitCode::OK. false - ExitCode::UNSPECIFIED_ERROR. Default: null. Available since 2.0.36. | yii\ |
$traceLine | string | Trace line with placeholders to be be substituted. Default: '{html}'. Available since 2.0.14. | ErrorHandler |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__clone() | This method is called after the object is created by cloning an existing one. | yii\ |
__get() | Returns the value of a component property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets the value of a component property. | yii\ |
__unset() | Sets a component property to be null. | yii\ |
addTypeLinks() | Adds informational links to the given PHP type/class. | ErrorHandler |
argumentsToString() | Converts arguments array to its string representation. | ErrorHandler |
attachBehavior() | Attaches a behavior to this component. | yii\ |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\ |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
clearOutput() | Removes all output echoed before calling this method. | yii\ |
convertExceptionToString() static | Converts an exception into a simple string. | yii\ |
convertExceptionToVerboseString() static | Converts an exception into a string that has verbose information about the exception and its trace. | yii\ |
createFrameworkVersionLink() | Creates string containing HTML link which refers to the page with the current version of the framework and version number text. | ErrorHandler |
createHttpStatusLink() | Creates HTML containing link to the page with the information on given HTTP status code. | ErrorHandler |
createServerInformationLink() | Creates string containing HTML link which refers to the home page of determined web-server software and its full name. | ErrorHandler |
detachBehavior() | Detaches a behavior from the component. | yii\ |
detachBehaviors() | Detaches all behaviors from the component. | yii\ |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\ |
getBehavior() | Returns the named behavior object. | yii\ |
getBehaviors() | Returns all behaviors attached to this component. | yii\ |
getExceptionName() | Returns human-readable exception name. | ErrorHandler |
handleError() | Handles PHP execution errors such as warnings and notices. | yii\ |
handleException() | Handles uncaught PHP exceptions. | yii\ |
handleFatalError() | Handles fatal PHP errors. | yii\ |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\ |
htmlEncode() | Converts special characters to HTML entities. | ErrorHandler |
init() | yii\ | |
isCoreFile() | Determines whether given name of the file belongs to the framework. | ErrorHandler |
logException() | Logs the given exception. | yii\ |
off() | Detaches an existing event handler from this component. | yii\ |
on() | Attaches an event handler to an event. | yii\ |
register() | Register this error handler. | yii\ |
renderCallStack() | Renders call stack. | ErrorHandler |
renderCallStackItem() | Renders a single call stack element. | ErrorHandler |
renderFile() | Renders a view file as a PHP script. | ErrorHandler |
renderPreviousExceptions() | Renders the previous exception stack for a given Exception. | ErrorHandler |
renderRequest() | Renders the global variables of the request. | ErrorHandler |
trigger() | Triggers an event. | yii\ |
unregister() | Unregisters this error handler by restoring the PHP error and exception handlers. | yii\ |
convertExceptionToArray() protected | Converts an exception into an array. | ErrorHandler |
getTypeUrl() protected | Returns the informational link URL for a given PHP type/class. | ErrorHandler |
handleFallbackExceptionMessage() protected | Handles exception thrown during exception processing in handleException(). | yii\ |
renderException() protected | Renders the exception. | ErrorHandler |
shouldRenderSimpleHtml() protected | ErrorHandler | |
triggerAfterRender() protected | Triggers EVENT_AFTER_RENDER and returns final HTML content. | ErrorHandler |
Events
Section titled “Events”| Event | Type | Description | Defined by |
|---|---|---|---|
EVENT_SHUTDOWN | yii\ | An event that is triggered when the handler is called by shutdown function via handleFatalError(). Event name: 'shutdown'. Available since 2.0.46. | yii\ |
Constants
Section titled “Constants”| Constant | Value | Description | Defined by |
|---|---|---|---|
EVENT_AFTER_RENDER | 'afterRender' | ErrorHandlerRenderEvent an event that is triggered after HTML error content is rendered. Available since 22.0. | ErrorHandler |
Property details
Section titled “Property details”$displayVars
Section titled “$displayVars ”public array $displayVars = ['_GET', '_POST', '_FILES', '_COOKIE', '_SESSION']List of the PHP predefined variables that should be displayed on the error page.
Note that a variable must be accessible via $GLOBALS. Otherwise it won’t be displayed.
Defaults to ['_GET', '_POST', '_FILES', '_COOKIE', '_SESSION'].
Since
2.0.7
$errorAction
Section titled “$errorAction ”public string|null $errorAction = nullThe route (e.g. site/error) to the controller action that will be used to display external errors.
Inside the action, it can retrieve the error information
using Yii::$app->errorHandler->exception. This property defaults to null, meaning ErrorHandler
will handle the error display.
$maxSourceLines
Section titled “$maxSourceLines ”public int $maxSourceLines = 19Maximum number of source code lines to be displayed.
Defaults to 19.
$maxTraceSourceLines
Section titled “$maxTraceSourceLines ”public int $maxTraceSourceLines = 13Maximum number of trace source code lines to be displayed.
Defaults to 13.
$traceLine
Section titled “$traceLine ”public string $traceLine = '{html}'Trace line with placeholders to be be substituted.
The placeholders are {file}, {line} and {text} and the string should be as follows.
File: {file} - Line: {line} - Text: {text}
Since
2.0.14
Method details
Section titled “Method details”addTypeLinks()
Section titled “addTypeLinks() ”public function addTypeLinks(string $code): stringAdds informational links to the given PHP type/class.
Parameters
| Parameter | Type | Description |
|---|---|---|
$code | string | Type/class name to be linkified. |
Returns
string: Linkified with HTML type/class name.
argumentsToString()
Section titled “argumentsToString() ”public function argumentsToString(array $args): stringConverts arguments array to its string representation.
Parameters
| Parameter | Type | Description |
|---|---|---|
$args | array | Arguments array to be converted |
Returns
string: String representation of the arguments array
createFrameworkVersionLink()
Section titled “createFrameworkVersionLink() ”public function createFrameworkVersionLink(): stringCreates string containing HTML link which refers to the page with the current version of the framework and version number text.
Returns
string: Framework version information hyperlink.
createHttpStatusLink()
Section titled “createHttpStatusLink() ”public function createHttpStatusLink(int $statusCode, string $statusDescription): stringCreates HTML containing link to the page with the information on given HTTP status code.
Parameters
| Parameter | Type | Description |
|---|---|---|
$statusCode | int | To be used to generate information link. |
$statusDescription | string | Description to display after the the status code. |
Returns
string: Generated HTML with HTTP status code information.
createServerInformationLink()
Section titled “createServerInformationLink() ”public function createServerInformationLink(): stringCreates string containing HTML link which refers to the home page of determined web-server software and its full name.
Returns
string: Server software information hyperlink.
getExceptionName()
Section titled “getExceptionName() ”public function getExceptionName(Throwable $exception): string|nullReturns human-readable exception name.
Parameters
| Parameter | Type | Description |
|---|---|---|
$exception | Throwable |
Returns
string|null: Human-readable exception name or null if it cannot be determined
htmlEncode()
Section titled “htmlEncode() ”public function htmlEncode(string $text): stringConverts special characters to HTML entities.
Parameters
| Parameter | Type | Description |
|---|---|---|
$text | string | To encode. |
Returns
string: Encoded original text.
isCoreFile()
Section titled “isCoreFile() ”public function isCoreFile(string $file): boolDetermines whether given name of the file belongs to the framework.
Parameters
| Parameter | Type | Description |
|---|---|---|
$file | string | Name to be checked. |
Returns
bool: Whether given name of the file belongs to the framework.
renderCallStack()
Section titled “renderCallStack() ”public function renderCallStack(Throwable $exception): stringRenders call stack.
Parameters
| Parameter | Type | Description |
|---|---|---|
$exception | Throwable | Exception to get call stack from |
Returns
string: HTML content of the rendered call stack.
Since
2.0.12
renderCallStackItem()
Section titled “renderCallStackItem() ”public function renderCallStackItem(string|null $file, int|null $line, string|null $class, string|null $method, array $args, int $index): stringRenders a single call stack element.
Parameters
| Parameter | Type | Description |
|---|---|---|
$file | string| | Name where call has happened. |
$line | int| | Number on which call has happened. |
$class | string| | Called class name. |
$method | string| | Called function/method name. |
$args | array | Array of method arguments. |
$index | int | Number of the call stack element. |
Returns
string: HTML content of the rendered call stack element.
renderFile()
Section titled “renderFile() ”public function renderFile(string $_file_, array $_params_): stringRenders a view file as a PHP script.
Parameters
| Parameter | Type | Description |
|---|---|---|
$_file_ | string | The view file. |
$_params_ | array | The parameters (name-value pairs) that will be extracted and made available in the view file. |
Returns
string: The rendering result
renderPreviousExceptions()
Section titled “renderPreviousExceptions() ”public function renderPreviousExceptions(Throwable $exception): stringRenders the previous exception stack for a given Exception.
Parameters
| Parameter | Type | Description |
|---|---|---|
$exception | Throwable | The exception whose precursors should be rendered. |
Returns
string: HTML content of the rendered previous exceptions. Empty string if there are none.
renderRequest()
Section titled “renderRequest() ”public function renderRequest(): stringRenders the global variables of the request.
List of global variables is defined in $displayVars.
convertExceptionToArray()
Section titled “convertExceptionToArray() ”protected function convertExceptionToArray(Throwable $exception): arrayConverts an exception into an array.
Parameters
| Parameter | Type | Description |
|---|---|---|
$exception | Throwable | The exception being converted |
Returns
array: The array representation of the exception.
getTypeUrl()
Section titled “getTypeUrl() ”protected function getTypeUrl(string $class, string|null $method): string|nullReturns the informational link URL for a given PHP type/class.
Parameters
| Parameter | Type | Description |
|---|---|---|
$class | string | The type or class name. |
$method | string| | The method name. |
Returns
string|null: The informational link URL.
See also
renderException()
Section titled “renderException() ”protected function renderException(Throwable $exception): mixedRenders the exception.
shouldRenderSimpleHtml()
Section titled “shouldRenderSimpleHtml() ”protected function shouldRenderSimpleHtml(): boolReturns
bool: If simple HTML should be rendered
Since
2.0.12
triggerAfterRender()
Section titled “triggerAfterRender() ”protected function triggerAfterRender(Throwable $exception, string $output): stringTriggers EVENT_AFTER_RENDER and returns final HTML content.
Parameters
| Parameter | Type | Description |
|---|---|---|
$exception | Throwable | The exception being rendered. |
$output | string | The rendered HTML output. |
Returns
string: The final HTML output.
Since
22.0