Class yii\web\ErrorAction

Inheritanceyii\web\ErrorAction » yii\base\Action » yii\base\Component » yii\base\BaseObject
Implementsyii\base\Configurable
Available since version2.0
Source Code https://github.com/yiisoft/yii2/blob/master/framework/web/ErrorAction.php

ErrorAction displays application errors using a specified view.

To use ErrorAction, you need to do the following steps:

First, declare an action of ErrorAction type in the actions() method of your SiteController class (or whatever controller you prefer), like the following:

public function actions()
{
    return [
        'error' => ['class' => 'yii\web\ErrorAction'],
    ];
}

Then, create a view file for this action. If the route of your error action is site/error, then the view file should be views/site/error.php. In this view file, the following variables are available:

  • $name: the error name
  • $message: the error message
  • $exception: the exception being handled

Finally, configure the "errorHandler" application component as follows,

'errorHandler' => [
    'errorAction' => 'site/error',
]

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$behaviors yii\base\Behavior[] List of behaviors attached to this component. This property is read-only. yii\base\Component
$controller yii\base\Controller|yii\web\Controller|yii\console\Controller The controller that owns this action yii\base\Action
$defaultMessage string The message to be displayed when the exception message contains sensitive information. yii\web\ErrorAction
$defaultName string The name of the error when the exception name cannot be determined. yii\web\ErrorAction
$id string ID of the action yii\base\Action
$layout string|false|null The name of the layout to be applied to this error action view. yii\web\ErrorAction
$uniqueId string The unique ID of this action among the whole application. This property is read-only. yii\base\Action
$view string The view file to be rendered. yii\web\ErrorAction

Protected Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$exception Exception The exception object, normally is filled on init() method call. yii\web\ErrorAction

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. yii\base\BaseObject
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\base\Action
__get() Returns the value of an object property. yii\base\BaseObject
__isset() Checks if a property is set, i.e. defined and not null. yii\base\BaseObject
__set() Sets value of an object property. yii\base\BaseObject
__unset() Sets an object property to null. yii\base\BaseObject
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\BaseObject
canSetProperty() Returns a value indicating whether a property can be set. yii\base\BaseObject
className() Returns the fully qualified name of this class. yii\base\BaseObject
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
getUniqueId() Returns the unique ID of this action among the whole application. yii\base\Action
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\BaseObject
hasProperty() Returns a value indicating whether a property is defined. yii\base\BaseObject
init() Initializes the object. yii\web\ErrorAction
off() Detaches an existing event handler from this component. yii\base\Component
on() Attaches an event handler to an event. yii\base\Component
run() Runs the action. yii\web\ErrorAction
runWithParams() Runs this action with the specified parameters. yii\base\Action
trigger() Triggers an event. yii\base\Component

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
afterRun() This method is called right after run() is executed. yii\base\Action
beforeRun() This method is called right before run() is executed. yii\base\Action
findException() Gets exception from the ErrorHandler component. yii\web\ErrorAction
getExceptionCode() Gets the code from the $exception. yii\web\ErrorAction
getExceptionMessage() Returns the $exception message for yii\base\UserException only. yii\web\ErrorAction
getExceptionName() Returns the exception name, followed by the code (if present). yii\web\ErrorAction
getViewRenderParams() Builds array of parameters that will be passed to the view. yii\web\ErrorAction
renderAjaxResponse() Builds string that represents the exception. yii\web\ErrorAction
renderHtmlResponse() Renders a view that represents the exception. yii\web\ErrorAction

Property Details

$defaultMessage public property

The message to be displayed when the exception message contains sensitive information. Defaults to "An internal server error occurred.".

public string $defaultMessage null
$defaultName public property

The name of the error when the exception name cannot be determined. Defaults to "Error".

public string $defaultName null
$exception protected property (available since version 2.0.11)

The exception object, normally is filled on init() method call.

See also [[findException()]] to know default way of obtaining exception.

protected Exception $exception null
$layout public property (available since version 2.0.14)

The name of the layout to be applied to this error action view. If not set, the layout configured in the controller will be used.

See also yii\base\Controller::$layout.

public string|false|null $layout null
$view public property

The view file to be rendered. If not set, it will take the value of $id. That means, if you name the action as "error" in "SiteController", then the view name would be "error", and the corresponding view file would be "views/site/error.php".

public string $view null

Method Details

findException() protected method (available since version 2.0.11)

Gets exception from the ErrorHandler component.

In case there is no exception in the component, treat as the action has been invoked not from error handler, but by direct route, so '404 Not Found' error will be displayed.

protected Exception findException ( )
getExceptionCode() protected method (available since version 2.0.11)

Gets the code from the $exception.

protected mixed getExceptionCode ( )
getExceptionMessage() protected method (available since version 2.0.11)

Returns the $exception message for yii\base\UserException only.

For other cases $defaultMessage will be returned.

protected string getExceptionMessage ( )
getExceptionName() protected method (available since version 2.0.11)

Returns the exception name, followed by the code (if present).

protected string getExceptionName ( )
getViewRenderParams() protected method (available since version 2.0.11)

Builds array of parameters that will be passed to the view.

protected array getViewRenderParams ( )
init() public method

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

public void init ( )
renderAjaxResponse() protected method (available since version 2.0.11)

Builds string that represents the exception.

Normally used to generate a response to AJAX request.

protected string renderAjaxResponse ( )
renderHtmlResponse() protected method (available since version 2.0.11)

Renders a view that represents the exception.

protected string renderHtmlResponse ( )
run() public method

Runs the action.

public string run ( )
return string

Result content