Skip to content
PHP Ecosystem
Explore

ErrorAction

API reference › yii › web › ErrorAction

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() rather than Module::$actionMap.

PropertyTypeDescriptionDefined by
$behaviors read-onlyarray<yii\base\Behavior<static>>List of behaviors attached to this component.yii\base\Component
$controlleryii\base\T|nullThe controller that owns this action, or null when the action runs standalone. via Module::$actionMap Default: null.yii\base\Action
$defaultMessagestringThe message to be displayed when the exception message contains sensitive information. Default: null.ErrorAction
$defaultNamestringThe name of the error when the exception name cannot be determined. Default: null.ErrorAction
$idstring|nullID of the action; null between construction and dispatcher post-assignment for standalone actions resolved through Module::$actionMap or Module::$actionNamespace. Default: null.yii\base\Action
$layoutstring|null|falseThe name of the layout to be applied to this error action view. Default: null. Available since 2.0.14.ErrorAction
$moduleyii\base\Module|nullThe owning module, or null if neither a module nor a controller is attached. Available since 22.0.yii\base\Action
$uniqueId read-onlystringThe unique ID of this action among the whole application.yii\base\Action
$viewstring|nullThe view file to be rendered. Default: null.ErrorAction
$exception protectedThrowableThe exception object, normally is filled on init() method call. Default: null. Available since 2.0.11.ErrorAction
MethodDescriptionDefined by
__construct()Constructor.yii\base\Action
__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
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
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
getModule()Returns the module that owns this action when invoked standalone.yii\base\Action
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\Component
hasProperty()Returns a value indicating whether a property is defined for this component.yii\base\Component
init()Initializes the object.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.ErrorAction
runWithParams()Runs this action with the specified parameters.yii\base\Action
setModule()Sets the module that owns this action when invoked standalone.yii\base\Action
trigger()Triggers an event.yii\base\Component
afterRun() protectedThis method is called right after run() is executed.yii\base\Action
beforeRun() protectedThis method is called right before run() is executed.yii\base\Action
findException() protectedGets exception from the ErrorHandler component.ErrorAction
getExceptionCode() protectedGets the code from the $exception.ErrorAction
getExceptionMessage() protectedReturns the $exception message for UserException only.ErrorAction
getExceptionName() protectedReturns the exception name, followed by the code (if present).ErrorAction
getViewRenderParams() protectedBuilds array of parameters that will be passed to the view.ErrorAction
renderAjaxResponse() protectedBuilds string that represents the exception.ErrorAction
renderHtmlResponse() protectedRenders a view that represents the exception.ErrorAction
resolveStandaloneParams() protectedResolves typed parameters of run() when the action is invoked without a hosting controller.yii\base\Action
public string $defaultMessage = null

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

Defaults to “An internal server error occurred.”.

public string $defaultName = null

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

Defaults to “Error”.

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

public string|null $view = null

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

public function run(): string

Runs the action.

Returns

string: Result content

protected function findException(): Throwable

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.

Returns

Throwable

Since

2.0.11

protected function getExceptionCode(): mixed

Gets the code from the $exception.

Returns

mixed

Since

2.0.11

protected function getExceptionMessage(): string

Returns the $exception message for UserException only.

For other cases $defaultMessage will be returned.

Returns

string

Since

2.0.11

protected function getExceptionName(): string

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

Returns

string

Since

2.0.11

protected function getViewRenderParams(): array

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

Returns

array

Since

2.0.11

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

protected function renderHtmlResponse(): string

Renders a view that represents the exception.

Returns

string

Since

2.0.11