Difference between #2 and #1 of Display a nice exception message on ajax requests

unchanged
Title
Display a nice exception message on ajax requests
unchanged
Category
Tips
unchanged
Tags
error, exception
changed
Content
Basics
------
When an exception is thrown in our code like
~~~
[php]
throw new CHttpException(403, 'You are not authorized to perform this action.');
~~~
if it is a CHttpException or if YII_DEBUG is not true, the error message can be
displayed by using
[CErrorHandler::errorAction](http://www.yiiframework.com/doc/api/1.1/CErrorHandler#errorAction-detail
"errorAction"). In the default code generated by `yiic` this is set to
SiteController::actionError() by this settings in config/main.php
~~~
[php]
'errorHandler' => array(
    'errorAction' => 'site/error',
),
~~~

But
--- 

in Yii versions previous to 1.1.9 exceptions was handled differently during ajax
request and displayed by
[CApplication::displayException()](http://www.yiiframework.com/doc/api/1.1/CApplication#displayException-detail
"displayException()"). This way the exception message displayed on
ajax requests could not be customized. The 
The message for the CGridView delete request with YII_DEBUG set to true
looked like
![old exception message on ajax
request](http://i1107.photobucket.com/albums/h397/mdomba/yiierror-old.jpg
"old exception message on ajax request")

In Yii version 1.1.9 the check for ajax request has been removed so now the
exception message is handled by
[CErrorHandler::errorAction](http://www.yiiframework.com/doc/api/1.1/CErrorHandler#errorAction-detail
"errorAction") even during ajax requests. 

This way the message can be customized for ajax requests.

Example
-------
by using this code
~~~
[php]
public function actionError()
{
    if($error=Yii::app()->errorHandler->error)
    {
    	if(Yii::app()->request->isAjaxRequest)
    		echo $error['message'];
    	else
        	$this->render('error', $error);
    }
}
~~~
the exception message for CGridView delete request will look like
![new exception message on ajax
request](http://i1107.photobucket.com/albums/h397/mdomba/yiierror-new.jpg
"new exception message on ajax request")