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.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$behaviors read-only | array<yii\<static>> | List of behaviors attached to this component. | yii\ |
$controller | yii\ | The controller that owns this action, or null when the action runs standalone. via Module::$actionMap Default: null. | yii\ |
$defaultMessage | string | The message to be displayed when the exception message contains sensitive information. Default: null. | ErrorAction |
$defaultName | string | The name of the error when the exception name cannot be determined. Default: null. | ErrorAction |
$id | string| | ID of the action; null between construction and dispatcher post-assignment for standalone actions resolved through Module::$actionMap or Module::$actionNamespace. Default: null. | yii\ |
$layout | string| | The name of the layout to be applied to this error action view. Default: null. Available since 2.0.14. | ErrorAction |
$module | yii\| | The owning module, or null if neither a module nor a controller is attached. Available since 22.0. | yii\ |
$uniqueId read-only | string | The unique ID of this action among the whole application. | yii\ |
$view | string| | The view file to be rendered. Default: null. | ErrorAction |
$exception protected | Throwable | The exception object, normally is filled on init() method call. Default: null. Available since 2.0.11. | ErrorAction |
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\ |
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\ |
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\ |
getModule() | Returns the module that owns this action when invoked standalone. | yii\ |
getUniqueId() | Returns the unique ID of this action among the whole application. | 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\ |
init() | Initializes the object. | ErrorAction |
off() | Detaches an existing event handler from this component. | yii\ |
on() | Attaches an event handler to an event. | yii\ |
run() | Runs the action. | ErrorAction |
runWithParams() | Runs this action with the specified parameters. | yii\ |
setModule() | Sets the module that owns this action when invoked standalone. | yii\ |
trigger() | Triggers an event. | yii\ |
afterRun() protected | This method is called right after run() is executed. | yii\ |
beforeRun() protected | This method is called right before run() is executed. | yii\ |
findException() protected | Gets exception from the ErrorHandler component. | ErrorAction |
getExceptionCode() protected | Gets the code from the $exception. | ErrorAction |
getExceptionMessage() protected | Returns the $exception message for UserException only. | ErrorAction |
getExceptionName() protected | Returns the exception name, followed by the code (if present). | ErrorAction |
getViewRenderParams() protected | Builds array of parameters that will be passed to the view. | ErrorAction |
renderAjaxResponse() protected | Builds string that represents the exception. | ErrorAction |
renderHtmlResponse() protected | Renders a view that represents the exception. | ErrorAction |
resolveStandaloneParams() protected | Resolves typed parameters of run() when the action is invoked without a hosting controller. | yii\ |
Property details
Section titled “Property details”$defaultMessage
Section titled “$defaultMessage ”public string $defaultMessage = nullThe message to be displayed when the exception message contains sensitive information.
Defaults to “An internal server error occurred.”.
$defaultName
Section titled “$defaultName ”public string $defaultName = nullThe name of the error when the exception name cannot be determined.
Defaults to “Error”.
$layout
Section titled “$layout ”public string|null|false $layout = nullThe 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
Section titled “$view ”public string|null $view = nullThe 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”.
Method details
Section titled “Method details”init()
Section titled “init() ”public function init(): mixedInitializes the object.
This method is invoked at the end of the constructor after the object is initialized with the given configuration.
Returns
mixed
run()
Section titled “run() ”public function run(): stringRuns the action.
Returns
string: Result content
findException()
Section titled “findException() ”protected function findException(): ThrowableGets 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.
getExceptionCode()
Section titled “getExceptionCode() ”protected function getExceptionCode(): mixedGets the code from the $exception.
Returns
mixed
Since
2.0.11
getExceptionMessage()
Section titled “getExceptionMessage() ”protected function getExceptionMessage(): stringReturns the $exception message for UserException only.
For other cases $defaultMessage will be returned.
Returns
string
Since
2.0.11
getExceptionName()
Section titled “getExceptionName() ”protected function getExceptionName(): stringReturns the exception name, followed by the code (if present).
Returns
string
Since
2.0.11
getViewRenderParams()
Section titled “getViewRenderParams() ”protected function getViewRenderParams(): arrayBuilds array of parameters that will be passed to the view.
Returns
array
Since
2.0.11
renderAjaxResponse()
Section titled “renderAjaxResponse() ”protected function renderAjaxResponse(): stringBuilds string that represents the exception.
Normally used to generate a response to AJAX request.
Returns
string
Since
2.0.11
renderHtmlResponse()
Section titled “renderHtmlResponse() ”protected function renderHtmlResponse(): stringRenders a view that represents the exception.
Returns
string
Since
2.0.11