Error Handler (Site/error) Not Working

I have this in my main.php




'errorHandler'=>array(

			// use 'site/error' action to display errors

            'errorAction'=>'site/error',

        ),



I have setup some accessFilters in Controller.php

Everything works except the site/error error handler.

Yii displays the error on a white page …





CHttpException


You are not authorized to perform this action. (/Applications/MAMP/yii/framework/web/auth/CAccessControlFilter.php:170)


#0 /Applications/MAMP/yii/framework/web/auth/CAccessControlFilter.php(133): CAccessControlFilter->accessDenied(Object(CWebUser), 'You are not aut...')

#1 /Applications/MAMP/yii/framework/web/filters/CFilter.php(39): CAccessControlFilter->preFilter(Object(CFilterChain))

#2 /Applications/MAMP/yii/framework/web/CController.php(1146): CFilter->filter(Object(CFilterChain))

#3 /Applications/MAMP/yii/framework/web/filters/CInlineFilter.php(59): CController->filterAccessControl(Object(CFilterChain))

#4 /Applications/MAMP/yii/framework/web/filters/CFilterChain.php(131): CInlineFilter->filter(Object(CFilterChain))

#5 /Applications/MAMP/yii/framework/web/CController.php(292): CFilterChain->run()

#6 /Applications/MAMP/yii/framework/web/CController.php(266): CController->runActionWithFilters(Object(CInlineAction), Array)

#7 /Applications/MAMP/yii/framework/web/CWebApplication.php(283): CController->run('error')

#8 /Applications/MAMP/yii/framework/base/CErrorHandler.php(332): CWebApplication->runController('site/error')

#9 /Applications/MAMP/yii/framework/base/CErrorHandler.php(205): CErrorHandler->render('error', Array)

#10 /Applications/MAMP/yii/framework/base/CErrorHandler.php(130): CErrorHandler->handleException(Object(CHttpException))

#11 /Applications/MAMP/yii/framework/base/CApplication.php(713): CErrorHandler->handle(Object(CExceptionEvent))

#12 [internal function]: CApplication->handleException(Object(CHttpException))

#13 {main}




But it is not allowing me to customize the error messages according to site/error, its like its not using the site/error at all, any ideas why?

I suspect that you haven’t made the error action accessible to non-authenticated users.

Make sure that in your Controller class, you have a rule to allow access to that action:




    public function accessRules()

    {

        return array(

            array('allow',

                'controllers'=>array('site'),

                'actions'=>array('error'),

            ),


            // More rules...


            array('deny'),

        );

    }



Of course!

Thanks I will try that now.

It works thanks.

Hi,

I am new in Yii. Need your help.

In which Controller , have to mention accessRules

If you’ve used the console to create a basic web app, there will be a file called Controller.php in your protected/components folder. All of your controllers will extend from this class, so you can put global access controls in there if you like.

Thanks for reply. I had added this code in my but still that doesn’t work for me. Same above message is coming instead of error paga.

Please check below code.

Thanks

/**

  • Controller is the customized base controller class.

  • All controller classes for this application should extend from this base class.

*/

class Controller extends CController

{

/**


 * @var string the default layout for the controller view. Defaults to '//layouts/column1',


 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.


 */


public $layout='//layouts/column1';


/**


 * @var array context menu items. This property will be assigned to {@link CMenu::items}.


 */


public $menu=array();


/**


 * @var array the breadcrumbs of the current page. The value of this property will


 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}


 * for more details on how to specify this property.


 */


public $breadcrumbs=array();





public function filters()


{


	return array(


		'accessControl',


	);


}





public function accessRules()


{


    return array(


        array('allow',


            'controllers'=>array('site'),


            'actions'=>array('error'),


        ),





        // More rules...





        array('deny'),


    );


}

}

Can you post the exact error?

Also, use code tags ("<>" in the editor) when posting code so it’s easier to read.

I am getting this error.





CHttpException


You are not authorized to perform this action. (C:\wamp\www\yii\framework\web\auth\CAccessControlFilter.php:182)


#0 C:\wamp\www\yii\framework\web\auth\CAccessControlFilter.php(145): CAccessControlFilter->accessDenied(Object(CWebUser), 'You are not aut...')

#1 C:\wamp\www\yii\framework\web\filters\CFilter.php(38): CAccessControlFilter->preFilter(Object(CFilterChain))

#2 C:\wamp\www\yii\framework\web\CController.php(1145): CFilter->filter(Object(CFilterChain))

#3 C:\wamp\www\yii\framework\web\filters\CInlineFilter.php(58): CController->filterAccessControl(Object(CFilterChain))

#4 C:\wamp\www\yii\framework\web\filters\CFilterChain.php(130): CInlineFilter->filter(Object(CFilterChain))

#5 C:\wamp\www\yii\framework\web\CController.php(291): CFilterChain->run()

#6 C:\wamp\www\yii\framework\web\CController.php(265): CController->runActionWithFilters(Object(CInlineAction), Array)

#7 C:\wamp\www\yii\framework\web\CWebApplication.php(282): CController->run('error')

#8 C:\wamp\www\yii\framework\base\CErrorHandler.php(331): CWebApplication->runController('site/error')

#9 C:\wamp\www\yii\framework\base\CErrorHandler.php(204): CErrorHandler->render('error', Array)

#10 C:\wamp\www\yii\framework\base\CErrorHandler.php(129): CErrorHandler->handleException(Object(CHttpException))

#11 C:\wamp\www\yii\framework\base\CApplication.php(732): CErrorHandler->handle(Object(CExceptionEvent))

#12 [internal function]: CApplication->handleException(Object(CHttpException))




And If i will change to index instead of error, it will work fine. But not with error action.





<?php




'errorHandler'=>array(

			// use 'site/error' action to display errors

			'errorAction'=>'site/index',

		),

?>




Are your controller classes all definitely extending Controller instead of CController?

Yes. Even I have each controller have there own access level. So mention both accessRules and filters functions in my all controllers.





<?php


class UserController extends Controller

{

	/**

	 * @return array action filters

	 */

	public function filters()

	{

		return array(

			'accessControl',

		);

	}


    public function accessRules()

    {

    }










<?php

/**

 * Controller is the customized base controller class.

 * All controller classes for this application should extend from this base class.

 */

class Controller extends CController

{

	/**

	 * @var string the default layout for the controller view. Defaults to '//layouts/column1',

	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.

	 */

	public $layout='//layouts/column1';

	/**

	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.

	 */

	public $menu=array();

	/**

	 * @var array the breadcrumbs of the current page. The value of this property will

	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}

	 * for more details on how to specify this property.

	 */

	public $breadcrumbs=array();


	public function filters()

	{

		return array(

			'accessControl',

		);

	}


	public function accessRules()

    {

        return array(

            array('allow',

                'controllers'=>array('site'),

                'actions'=>array('error'),

            ),


            // More rules...


            array('deny'),

        );

    }

}




There’s the problem; you’re overriding your rules each time, so the rules in Controller aren’t being used. I’m not sure of the best way to merge in the rules from the parent. The easiest option would be to just put all of your access rules in Controller rather than breaking them up. It’s all in one place then.

Resolved the issue. Now proper error message is displaying.

Thanks for the help.