Yii Framework official guide series 46-topics: Error Handling

Source: Internet
Author: User
Yii provides a complete error handling mechanism based on PHP5 exception handling. When an application starts to run and processes user requests, the handleError method is registered to process PHPwarnings and notices information...



Yii provides a complete error handling mechanism based on PHP5 exception handling. When an application starts to run and processes user requests, it registers the handleError method to process PHP warnings and notices information. it also registers the handleException method to handle uncaptured PHP exceptions. Therefore, if a PHP warning/notice or an uncaptured PHP exception occurs during application running, the error processor takes control of the exception to run the necessary processing mechanism.

Tip:The error processor registration is performed in the constructor method in the application. the set_exception_handler and set_error_handler functions are used. If you do not want Yii to handle errors and exceptions, you can defineYII_ENABLE_ERROR_HANDLERAndYII_ENABLE_EXCEPTION_HANDLERFalse.

By default, when an onError event (or onException event) is triggered, errorHandler (or exceptionHandler) is triggered. If the error or exception is not handled by any event, you need to run the errorHandler component to handle it.

1. exception

The exception thrown in Yii is no different from that in a common PHP file. You can use the following code to throw an exception:


Throw new ExceptionClass ('error information ');

Yii defines two exception classes: CException and CHttpException. The former is a common exception class, and the latter is used to display exception information to end users. At the same time, the latter has a statusCode attribute to represent the HTTP status code. The exception type determines the display effect.

Tip:If you want to tell the user that an operation is incorrect, it is the easiest way to trigger a CHttpException. For example, if you provide an invalid ID value in the URL, we can display a 404 error:


// If the submitted ID is invalid throw new CHttpException (404, 'This page does not exist ');

2. display error

When an error is forwarded to the CErrorHandler component, it selects an appropriate view to display the error. If this error is to be displayed to the end user (for example, a CHttpException ),errorXXXTo display errors. ThisXXXIndicates the HTTP error code (such as 400,404,500 ). If this is an internal error, the View name isexception. In the latter method, the complete call stack information and error line information are displayed.

Information:When an application runs in production mode, view is used for all errors, including internal errors.errorXXX. This is because the stack information and error line information of the call may contain some sensitive information. In this case, the developer should rely on the error log to determine the cause of the error.

CErrorHandler searches for appropriate views to display error information. the search order is as follows:

  1. WebRoot/themes/ThemeName/views/system: In the current topic viewsystemDirectory.

  2. WebRoot/protected/views/system: In the default view of the applicationsystemDirectory.

  3. yii/framework/views: In the standard view directory provided by Yii.

Therefore, if you want to display custom errors, you can directlysystemView directory or topicsystemCreate a view file in the view directory. Each view file is a common PHP file that contains many HTML code. Reference FrameworkviewFor more information.

3. use an action to handle errors

Yii can also use controller actions to handle error display. The implementation method is to configure an error processor in the application configuration file.


return array(    ......    'components'=>array(        'errorHandler'=>array(            'errorAction'=>'site/error',        ),    ),);

In the above code, we configured the CErrorHandler: errorAction attribute, and the attribute value is a routesite/error. This route pointsSiteControllerInerror. You can also use other routes.

We can writeerrorAction:


public function actionError(){    if($error=Yii::app()->errorHandler->error)        $this->render('error', $error);}

In this action, obtain detailed error information from CErrorHandler: error. If the obtained information is not empty, use the information returned by CErrorHandler: error to render it.errorView. CErrorHandler: error returns an array with the following structure:

  • code: HTTP status code (such as 403,500 );

  • type: Error type (such as CHttpException,PHP Error);

  • message: Error message;

  • file: The PHP file name is incorrect;

  • line: The row where the error is located;

  • trace: Incorrect call stack information;

  • source: Context of the error code.

Tip:The reason for checking whether CErrorHandler: error is null iserrorActions can be accessed by users. at this time, there may be no errors. When we pass$errorArray to the view, which will be automatically released as independent variables. Therefore, we can use$code,$typeTo access this information.

4. message record

OneerrorLevel error information is recorded when an error occurs. If this error is caused by PHP warning or notice, the message will be recorded inphpIn this category, if the error message is caused by an uncaptured exception, the category will beexception.ExceptionClassName(For CHttpException, its statusCode will also be appended to the category name ). Developers can use these records to monitor error messages during application execution.

The above is the Yii Framework official guide series 46 -- special topic: Error handling content. For more information, see PHP Chinese website (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.