# ErrorAction

> ErrorAction displays application errors using a specified view.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [web](/docs/api/yii/web/index.md) › ErrorAction

| class | `yii\web\ErrorAction` |
| --- | --- |
| Extends | [`yii\base\Action`](/docs/api/yii/base/action/index.md) » [`yii\base\Component`](/docs/api/yii/base/component/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/web/ErrorAction.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/web/ErrorAction.php#L61) |

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',
]
```

Not standalone-compatible: this action requires a hosting controller because rendering, layout resolution, and `view`
aliases are performed through the controller. Register it via [`Controller::actions()`](/docs/api/yii/base/controller/index.md#method-actions) rather than
[`Module::$actionMap`](/docs/api/yii/base/module/index.md#property-actionMap).

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-behaviors"></span>[`$behaviors`](/docs/api/yii/base/component/index.md#property-behaviors) read-only | `array<`[`yii\base\Behavior`](/docs/api/yii/base/behavior/index.md)`<static>>` | List of behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="property-controller"></span>[`$controller`](/docs/api/yii/base/action/index.md#property-controller) | `yii\base\T\|null` | The controller that owns this action, or `null` when the action runs standalone. via [`Module::$actionMap`](/docs/api/yii/base/module/index.md#property-actionMap) Default: `null`. | [`yii\base\Action`](/docs/api/yii/base/action/index.md) |
| [`$defaultMessage`](#property-defaultMessage) | `string` | The message to be displayed when the exception message contains sensitive information. Default: `null`. | `ErrorAction` |
| [`$defaultName`](#property-defaultName) | `string` | The name of the error when the exception name cannot be determined. Default: `null`. | `ErrorAction` |
| <span id="property-id"></span>[`$id`](/docs/api/yii/base/action/index.md#property-id) | `string\|null` | ID of the action; `null` between construction and dispatcher post-assignment for standalone actions resolved through [`Module::$actionMap`](/docs/api/yii/base/module/index.md#property-actionMap) or [`Module::$actionNamespace`](/docs/api/yii/base/module/index.md#property-actionNamespace). Default: `null`. | [`yii\base\Action`](/docs/api/yii/base/action/index.md) |
| [`$layout`](#property-layout) | `string\|null\|false` | The name of the layout to be applied to this error action view. Default: `null`. Available since 2.0.14. | `ErrorAction` |
| <span id="property-module"></span>[`$module`](/docs/api/yii/base/action/index.md#property-module) | [`yii\base\Module`](/docs/api/yii/base/module/index.md)`\|null` | The owning module, or `null` if neither a module nor a controller is attached. Available since 22.0. | [`yii\base\Action`](/docs/api/yii/base/action/index.md) |
| <span id="property-uniqueId"></span>[`$uniqueId`](/docs/api/yii/base/action/index.md#property-uniqueId) read-only | `string` | The unique ID of this action among the whole application. | [`yii\base\Action`](/docs/api/yii/base/action/index.md) |
| [`$view`](#property-view) | `string\|null` | The view file to be rendered. Default: `null`. | `ErrorAction` |
| <span id="property-exception"></span>`$exception` protected | [`Throwable`](https://www.php.net/manual/en/class.throwable.php) | The exception object, normally is filled on [`init()`](/docs/api/yii/web/erroraction/index.md#method-init) method call. Default: `null`. Available since 2.0.11. | `ErrorAction` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/base/action/index.md#method-__construct) | Constructor. | [`yii\base\Action`](/docs/api/yii/base/action/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/component/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__clone"></span>[`__clone()`](/docs/api/yii/base/component/index.md#method-__clone) | This method is called after the object is created by cloning an existing one. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/component/index.md#method-__get) | Returns the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/component/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/component/index.md#method-__set) | Sets the value of a component property. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/component/index.md#method-__unset) | Sets a component property to be null. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehavior"></span>[`attachBehavior()`](/docs/api/yii/base/component/index.md#method-attachBehavior) | Attaches a behavior to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-attachBehaviors"></span>[`attachBehaviors()`](/docs/api/yii/base/component/index.md#method-attachBehaviors) | Attaches a list of behaviors to the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-behaviors"></span>[`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) | Returns a list of behaviors that this component should behave as. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/component/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/component/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-detachBehavior"></span>[`detachBehavior()`](/docs/api/yii/base/component/index.md#method-detachBehavior) | Detaches a behavior from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-detachBehaviors"></span>[`detachBehaviors()`](/docs/api/yii/base/component/index.md#method-detachBehaviors) | Detaches all behaviors from the component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-ensureBehaviors"></span>[`ensureBehaviors()`](/docs/api/yii/base/component/index.md#method-ensureBehaviors) | Makes sure that the behaviors declared in [`behaviors()`](/docs/api/yii/base/component/index.md#method-behaviors) are attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehavior"></span>[`getBehavior()`](/docs/api/yii/base/component/index.md#method-getBehavior) | Returns the named behavior object. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getBehaviors"></span>[`getBehaviors()`](/docs/api/yii/base/component/index.md#method-getBehaviors) | Returns all behaviors attached to this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-getModule"></span>[`getModule()`](/docs/api/yii/base/action/index.md#method-getModule) | Returns the module that owns this action when invoked standalone. | [`yii\base\Action`](/docs/api/yii/base/action/index.md) |
| <span id="method-getUniqueId"></span>[`getUniqueId()`](/docs/api/yii/base/action/index.md#method-getUniqueId) | Returns the unique ID of this action among the whole application. | [`yii\base\Action`](/docs/api/yii/base/action/index.md) |
| <span id="method-hasEventHandlers"></span>[`hasEventHandlers()`](/docs/api/yii/base/component/index.md#method-hasEventHandlers) | Returns a value indicating whether there is any handler attached to the named event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/component/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/component/index.md#method-hasProperty) | Returns a value indicating whether a property is defined for this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`init()`](#method-init) | Initializes the object. | `ErrorAction` |
| <span id="method-off"></span>[`off()`](/docs/api/yii/base/component/index.md#method-off) | Detaches an existing event handler from this component. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-on"></span>[`on()`](/docs/api/yii/base/component/index.md#method-on) | Attaches an event handler to an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| [`run()`](#method-run) | Runs the action. | `ErrorAction` |
| <span id="method-runWithParams"></span>[`runWithParams()`](/docs/api/yii/base/action/index.md#method-runWithParams) | Runs this action with the specified parameters. | [`yii\base\Action`](/docs/api/yii/base/action/index.md) |
| <span id="method-setModule"></span>[`setModule()`](/docs/api/yii/base/action/index.md#method-setModule) | Sets the module that owns this action when invoked standalone. | [`yii\base\Action`](/docs/api/yii/base/action/index.md) |
| <span id="method-trigger"></span>[`trigger()`](/docs/api/yii/base/component/index.md#method-trigger) | Triggers an event. | [`yii\base\Component`](/docs/api/yii/base/component/index.md) |
| <span id="method-afterRun"></span>[`afterRun()`](/docs/api/yii/base/action/index.md#method-afterRun) protected | This method is called right after `run()` is executed. | [`yii\base\Action`](/docs/api/yii/base/action/index.md) |
| <span id="method-beforeRun"></span>[`beforeRun()`](/docs/api/yii/base/action/index.md#method-beforeRun) protected | This method is called right before `run()` is executed. | [`yii\base\Action`](/docs/api/yii/base/action/index.md) |
| [`findException()`](#method-findException) protected | Gets exception from the [ErrorHandler](/docs/api/yii/web/errorhandler/index.md) component. | `ErrorAction` |
| [`getExceptionCode()`](#method-getExceptionCode) protected | Gets the code from the [`$exception`](/docs/api/yii/web/erroraction/index.md#property-exception). | `ErrorAction` |
| [`getExceptionMessage()`](#method-getExceptionMessage) protected | Returns the [`$exception`](/docs/api/yii/web/erroraction/index.md#property-exception) message for [`UserException`](/docs/api/yii/base/userexception/index.md) only. | `ErrorAction` |
| [`getExceptionName()`](#method-getExceptionName) protected | Returns the exception name, followed by the code (if present). | `ErrorAction` |
| [`getViewRenderParams()`](#method-getViewRenderParams) protected | Builds array of parameters that will be passed to the view. | `ErrorAction` |
| [`renderAjaxResponse()`](#method-renderAjaxResponse) protected | Builds string that represents the exception. | `ErrorAction` |
| [`renderHtmlResponse()`](#method-renderHtmlResponse) protected | Renders a view that represents the exception. | `ErrorAction` |
| <span id="method-resolveStandaloneParams"></span>[`resolveStandaloneParams()`](/docs/api/yii/base/action/index.md#method-resolveStandaloneParams) protected | Resolves typed parameters of `run()` when the action is invoked without a hosting controller. | [`yii\base\Action`](/docs/api/yii/base/action/index.md) |

## Property details

### $defaultMessage <span id="property-defaultMessage"></span>

```php
public string $defaultMessage = null
```

The message to be displayed when the exception message contains sensitive information.

Defaults to "An internal server error occurred.".

### $defaultName <span id="property-defaultName"></span>

```php
public string $defaultName = null
```

The name of the error when the exception name cannot be determined.

Defaults to "Error".

### $layout <span id="property-layout"></span>

```php
public string|null|false $layout = null
```

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.

**Since**

2.0.14

### $view <span id="property-view"></span>

```php
public string|null $view = null
```

The view file to be rendered.

If not set, it will take the value of [`$id`](/docs/api/yii/base/action/index.md#property-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".

## Method details

### init() <span id="method-init"></span>

```php
public function init(): mixed
```

Initializes the object.

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

**Returns**

`mixed`

### run() <span id="method-run"></span>

```php
public function run(): string
```

Runs the action.

**Returns**

`string`: Result content

### findException() <span id="method-findException"></span>

```php
protected function findException(): Throwable
```

Gets exception from the [ErrorHandler](/docs/api/yii/web/errorhandler/index.md) 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.

**Returns**

[`Throwable`](https://www.php.net/manual/en/class.throwable.php)

**Since**

2.0.11

### getExceptionCode() <span id="method-getExceptionCode"></span>

```php
protected function getExceptionCode(): mixed
```

Gets the code from the [`$exception`](/docs/api/yii/web/erroraction/index.md#property-exception).

**Returns**

`mixed`

**Since**

2.0.11

### getExceptionMessage() <span id="method-getExceptionMessage"></span>

```php
protected function getExceptionMessage(): string
```

Returns the [`$exception`](/docs/api/yii/web/erroraction/index.md#property-exception) message for [`UserException`](/docs/api/yii/base/userexception/index.md) only.

For other cases [`$defaultMessage`](/docs/api/yii/web/erroraction/index.md#property-defaultMessage) will be returned.

**Returns**

`string`

**Since**

2.0.11

### getExceptionName() <span id="method-getExceptionName"></span>

```php
protected function getExceptionName(): string
```

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

**Returns**

`string`

**Since**

2.0.11

### getViewRenderParams() <span id="method-getViewRenderParams"></span>

```php
protected function getViewRenderParams(): array
```

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

**Returns**

`array`

**Since**

2.0.11

### renderAjaxResponse() <span id="method-renderAjaxResponse"></span>

```php
protected function renderAjaxResponse(): string
```

Builds string that represents the exception.

Normally used to generate a response to AJAX request.

**Returns**

`string`

**Since**

2.0.11

### renderHtmlResponse() <span id="method-renderHtmlResponse"></span>

```php
protected function renderHtmlResponse(): string
```

Renders a view that represents the exception.

**Returns**

`string`

**Since**

2.0.11