Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.
Yii includes a built-in [[yii\web\ErrorHandler|error handler]] which makes error handling a much more pleasant
experience than before. In particular, the Yii error handler does the following to improve error handling:
All non-fatal PHP errors (e.g. warnings, notices) are converted into catchable exceptions.
Exceptions and fatal PHP errors are displayed with detailed call stack information and source code lines
in debug mode.
The [[yii\web\ErrorHandler|error handler]] is enabled by default. You may disable it by defining the constant
YII_ENABLE_ERROR_HANDLER to be false in the entry script of your application.
The [[yii\web\ErrorHandler|error handler]] is registered as an application component named errorHandler.
You may configure it in the application configuration like the following:
return [
'components'=> [
'errorHandler'=> [
'maxSourceLines'=>20,
],
],
];
With the above configuration, the number of source code lines to be displayed in exception pages will be up to 20.
As aforementioned, the error handler turns all non-fatal PHP errors into catchable exceptions. This means you can
use the following code to deal with PHP errors:
useYii;
use yii\base\ErrorException;
try {
10/0;
} catch(ErrorException$e) {
Yii::warning("Division by zero.");
}
// execution continues...
If you want to show an error page telling the user that his request is invalid or unexpected, you may simply
throw an [[yii\web\HttpException|HTTP exception]], such as [[yii\web\NotFoundHttpException]]. The error handler
will correctly set the HTTP status code of the response and use an appropriate error view to display the error
message.
The [[yii\web\ErrorHandler|error handler]] adjusts the error display according to the value of the constant YII_DEBUG.
When YII_DEBUG is true (meaning in debug mode), the error handler will display exceptions with detailed call
stack information and source code lines to help easier debugging. And when YII_DEBUG is false, only the error
message will be displayed to prevent revealing sensitive information about the application.
Info: If an exception is a descendant of [[yii\base\UserException]], no call stack will be displayed regardless
the value of YII_DEBUG. This is because such exceptions are considered to be caused by user mistakes and the
developers do not need to fix anything.
By default, the [[yii\web\ErrorHandler|error handler]] displays errors using two views:
@yii/views/errorHandler/error.php: used when errors should be displayed WITHOUT call stack information.
When YII_DEBUG is false, this is the only error view to be displayed.
@yii/views/errorHandler/exception.php: used when errors should be displayed WITH call stack information.
You can configure the [[yii\web\ErrorHandler::errorView|errorView]] and [[yii\web\ErrorHandler::exceptionView|exceptionView]]
properties of the error handler to use your own views to customize the error display.
A better way of customizing the error display is to use dedicated error actions.
To do so, first configure the [[yii\web\ErrorHandler::errorAction|errorAction]] property of the errorHandler
component like the following:
return [
'components'=> [
'errorHandler'=> [
'errorAction'=>'site/error',
],
]
];
The [[yii\web\ErrorHandler::errorAction|errorAction]] property takes a route
to an action. The above configuration states that when an error needs to be displayed without call stack information,
the site/error action should be executed.
You can create the site/error action as follows,
namespace app\controllers;
useYii;
use yii\web\Controller;
classSiteControllerextendsController
{
publicfunctionactions()
{
return [
'error'=> [
'class'=>'yii\web\ErrorAction',
],
];
}
}
The above code defines the error action using the [[yii\web\ErrorAction]] class which renders an error
using a view named error.
Besides using [[yii\web\ErrorAction]], you may also define the error action using an action method like the following,
You should now create a view file located at views/site/error.php. In this view file, you can access
the following variables if the error action is defined as [[yii\web\ErrorAction]]:
name: the name of the error;
message: the error message;
exception: the exception object through which you can retrieve more useful information, such as HTTP status code,
error code, error call stack, etc.
The error handler displays errors according to the format setting of the response.
If the [[yii\web\Response::format|response format]] is html, it will use the error or exception view
to display errors, as described in the last subsection. For other response formats, the error handler will
assign the array representation of the exception to the [[yii\web\Response::data]] property which will then
be converted to different formats accordingly. For example, if the response format is json, you may see
the following response: