Yii error handler

Error handling is very important to provide user friendly application. Yii provides an easy way to handle errors in organized and customized way. Yii has a component CErrorHandler which provides the functionality of error handling. Also yii provides a way to handle exception using CHttpException.

 

Default Error handling configuration

CErrorHandler named as errorHandler component. It has default action to site/error.

 

site/error

 

We can get error using Yii::app()->errorHandler->error or Yii::app()->errorHandler->getError(). Both return an array containing the error details.

Returned array elements:

  1. code – the HTTP status code (e.g. 403, 500)
  2. type – the error type (e.g. ‘CHttpException’, ‘PHP Error’)
  3. message – the error message
  4. file – the name of the PHP script file where the error occurs
  5. line – the line number of the code where the error occurs
  6. trace – the call stack of the error
  7. source – the context source code where the error occurs

 

You can customize both the things as default error action & error method. It uses error view you can also customize this. This is the easy way to customize errors.

To understand more on yii error handling look at this diagram.

yii errors

Related posts:

About the author
Onkar Janwa

Onkar Janwa

Hello, Welcome to my blog "WayToBlogger". I am a Software Engineer, dedicated blogger, cricket player & a reader living in world famous Jaipur city of India. Here I am sharing my technical knowledge & funny things which readers will enjoy on reaching here.
This entry was posted in Yii and tagged , , . Bookmark the permalink.

One thought on “Yii error handler

  1. best online tv software says: September 16, 2012 at 12:58 am

    Great post! Thx.

Leave a Reply