Skip to content
PHP Ecosystem
Explore

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.

PropertyTypeDescriptionDefined by
$behaviors read-onlyarray<yii\base\Behavior<static>>List of behaviors attached to this component.yii\base\Component
$callStackItemViewstringThe path of the view file for rendering exceptions and errors call stack element. Default: '@yii/views/errorHandler/callStackItem.php'.ErrorHandler
$discardExistingOutputboolWhether to discard any existing page output before error display. Default: true.yii\base\ErrorHandler
$displayVarsarrayList of the PHP predefined variables that should be displayed on the error page. Default: ['_GET', '_POST', '_FILES', '_COOKIE', '_SESSION']. Available since 2.0.7.ErrorHandler
$errorActionstring|nullThe route (e.g. site/error) to the controller action that will be used to display external errors. Default: null.ErrorHandler
$errorViewstringThe path of the view file for rendering exceptions without call stack information. Default: '@yii/views/errorHandler/error.php'.ErrorHandler
$exceptionThrowable|nullThe exception that is being handled currently. Default: null.yii\base\ErrorHandler
$exceptionViewstringThe path of the view file for rendering exceptions. Default: '@yii/views/errorHandler/exception.php'.ErrorHandler
$maxSourceLinesintMaximum number of source code lines to be displayed. Default: 19.ErrorHandler
$maxTraceSourceLinesintMaximum number of trace source code lines to be displayed. Default: 13.ErrorHandler
$memoryReserveSizeintThe size of the reserved memory. Default: 262144.yii\base\ErrorHandler
$previousExceptionViewstringThe path of the view file for rendering previous exceptions. Default: '@yii/views/errorHandler/previousException.php'.ErrorHandler
$silentExitOnExceptionboolIf true - handleException() will finish script with ExitCode::OK. false - ExitCode::UNSPECIFIED_ERROR. Default: null. Available since 2.0.36.yii\base\ErrorHandler
$traceLinestringTrace line with placeholders to be be substituted. Default: '{html}'. Available since 2.0.14.ErrorHandler
MethodDescriptionDefined by
__construct()Constructor.yii\base\BaseObject
__call()Calls the named method which is not a class method.yii\base\Component
__clone()This method is called after the object is created by cloning an existing one.yii\base\Component
__get()Returns the value of a component property.yii\base\Component
__isset()Checks if a property is set, i.e. defined and not null.yii\base\Component
__set()Sets the value of a component property.yii\base\Component
__unset()Sets a component property to be null.yii\base\Component
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\base\Component
attachBehaviors()Attaches a list of behaviors to the component.yii\base\Component
behaviors()Returns a list of behaviors that this component should behave as.yii\base\Component
canGetProperty()Returns a value indicating whether a property can be read.yii\base\Component
canSetProperty()Returns a value indicating whether a property can be set.yii\base\Component
className() staticReturns the fully qualified name of this class.yii\base\BaseObject
clearOutput()Removes all output echoed before calling this method.yii\base\ErrorHandler
convertExceptionToString() staticConverts an exception into a simple string.yii\base\ErrorHandler
convertExceptionToVerboseString() staticConverts an exception into a string that has verbose information about the exception and its trace.yii\base\ErrorHandler
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\base\Component
detachBehaviors()Detaches all behaviors from the component.yii\base\Component
ensureBehaviors()Makes sure that the behaviors declared in behaviors() are attached to this component.yii\base\Component
getBehavior()Returns the named behavior object.yii\base\Component
getBehaviors()Returns all behaviors attached to this component.yii\base\Component
getExceptionName()Returns human-readable exception name.ErrorHandler
handleError()Handles PHP execution errors such as warnings and notices.yii\base\ErrorHandler
handleException()Handles uncaught PHP exceptions.yii\base\ErrorHandler
handleFatalError()Handles fatal PHP errors.yii\base\ErrorHandler
hasEventHandlers()Returns a value indicating whether there is any handler attached to the named event.yii\base\Component
hasMethod()Returns a value indicating whether a method is defined.yii\base\Component
hasProperty()Returns a value indicating whether a property is defined for this component.yii\base\Component
htmlEncode()Converts special characters to HTML entities.ErrorHandler
init()yii\base\ErrorHandler
isCoreFile()Determines whether given name of the file belongs to the framework.ErrorHandler
logException()Logs the given exception.yii\base\ErrorHandler
off()Detaches an existing event handler from this component.yii\base\Component
on()Attaches an event handler to an event.yii\base\Component
register()Register this error handler.yii\base\ErrorHandler
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\base\Component
unregister()Unregisters this error handler by restoring the PHP error and exception handlers.yii\base\ErrorHandler
convertExceptionToArray() protectedConverts an exception into an array.ErrorHandler
getTypeUrl() protectedReturns the informational link URL for a given PHP type/class.ErrorHandler
handleFallbackExceptionMessage() protectedHandles exception thrown during exception processing in handleException().yii\base\ErrorHandler
renderException() protectedRenders the exception.ErrorHandler
shouldRenderSimpleHtml() protectedErrorHandler
triggerAfterRender() protectedTriggers EVENT_AFTER_RENDER and returns final HTML content.ErrorHandler
EventTypeDescriptionDefined by
EVENT_SHUTDOWNyii\base\EventAn event that is triggered when the handler is called by shutdown function via handleFatalError(). Event name: 'shutdown'. Available since 2.0.46.yii\base\ErrorHandler
ConstantValueDescriptionDefined by
EVENT_AFTER_RENDER'afterRender'ErrorHandlerRenderEvent an event that is triggered after HTML error content is rendered. Available since 22.0.ErrorHandler
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

public string|null $errorAction = null

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

public int $maxSourceLines = 19

Maximum number of source code lines to be displayed.

Defaults to 19.

public int $maxTraceSourceLines = 13

Maximum number of trace source code lines to be displayed.

Defaults to 13.

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

public function addTypeLinks(string $code): string

Adds informational links to the given PHP type/class.

Parameters

ParameterTypeDescription
$codestringType/class name to be linkified.

Returns

string: Linkified with HTML type/class name.

public function argumentsToString(array $args): string

Converts arguments array to its string representation.

Parameters

ParameterTypeDescription
$argsarrayArguments array to be converted

Returns

string: String representation of the arguments array

public function createFrameworkVersionLink(): string

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

public function createHttpStatusLink(int $statusCode, string $statusDescription): string

Creates HTML containing link to the page with the information on given HTTP status code.

Parameters

ParameterTypeDescription
$statusCodeintTo be used to generate information link.
$statusDescriptionstringDescription to display after the the status code.

Returns

string: Generated HTML with HTTP status code information.

public function createServerInformationLink(): string

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

public function getExceptionName(Throwable $exception): string|null

Returns human-readable exception name.

Parameters

ParameterTypeDescription
$exceptionThrowable

Returns

string|null: Human-readable exception name or null if it cannot be determined

public function htmlEncode(string $text): string

Converts special characters to HTML entities.

Parameters

ParameterTypeDescription
$textstringTo encode.

Returns

string: Encoded original text.

public function isCoreFile(string $file): bool

Determines whether given name of the file belongs to the framework.

Parameters

ParameterTypeDescription
$filestringName to be checked.

Returns

bool: Whether given name of the file belongs to the framework.

public function renderCallStack(Throwable $exception): string

Renders call stack.

Parameters

ParameterTypeDescription
$exceptionThrowableException to get call stack from

Returns

string: HTML content of the rendered call stack.

Since

2.0.12

public function renderCallStackItem(string|null $file, int|null $line, string|null $class, string|null $method, array $args, int $index): string

Renders a single call stack element.

Parameters

ParameterTypeDescription
$filestring|nullName where call has happened.
$lineint|nullNumber on which call has happened.
$classstring|nullCalled class name.
$methodstring|nullCalled function/method name.
$argsarrayArray of method arguments.
$indexintNumber of the call stack element.

Returns

string: HTML content of the rendered call stack element.

public function renderFile(string $_file_, array $_params_): string

Renders a view file as a PHP script.

Parameters

ParameterTypeDescription
$_file_stringThe view file.
$_params_arrayThe parameters (name-value pairs) that will be extracted and made available in the view file.

Returns

string: The rendering result

public function renderPreviousExceptions(Throwable $exception): string

Renders the previous exception stack for a given Exception.

Parameters

ParameterTypeDescription
$exceptionThrowableThe exception whose precursors should be rendered.

Returns

string: HTML content of the rendered previous exceptions. Empty string if there are none.

public function renderRequest(): string

Renders the global variables of the request.

List of global variables is defined in $displayVars.

Returns

string: The rendering result

See also

protected function convertExceptionToArray(Throwable $exception): array

Converts an exception into an array.

Parameters

ParameterTypeDescription
$exceptionThrowableThe exception being converted

Returns

array: The array representation of the exception.

protected function getTypeUrl(string $class, string|null $method): string|null

Returns the informational link URL for a given PHP type/class.

Parameters

ParameterTypeDescription
$classstringThe type or class name.
$methodstring|nullThe method name.

Returns

string|null: The informational link URL.

See also

protected function renderException(Throwable $exception): mixed

Renders the exception.

Parameters

ParameterTypeDescription
$exceptionThrowableThe exception to be rendered.

Returns

mixed

protected function shouldRenderSimpleHtml(): bool

Returns

bool: If simple HTML should be rendered

Since

2.0.12

protected function triggerAfterRender(Throwable $exception, string $output): string

Triggers EVENT_AFTER_RENDER and returns final HTML content.

Parameters

ParameterTypeDescription
$exceptionThrowableThe exception being rendered.
$outputstringThe rendered HTML output.

Returns

string: The final HTML output.

Since

22.0