Customized views for CException

Hi,

I currently have the problem that I need customized views for CExceptions. While I know how to do this with CHttpException (creating views called errorXXX) I don’t know how to achieve this with a class extending CException. The reason is that I am working on an extension (ActiveResource for Yii) that throws custom Exceptions that display sensitive information to the developer, NOT to the end user (imagine a REST service error response with some data about uris and error bodies…I think that shouldn’t be seen by users)

I know I could change the system view “exception” in the framework folder but that isn’t a very elegant way as this view is perfectly fine for classic exceptions

Any hint?

Thanks in advance,

Haensel

You could throw your exceptions with some custom error code:


throw new CException('Private debug message....', 99);

If you configured a CErrorHandler::errorAction (see example config in any new webapp) you can check for that code in your error action:




 public function actionError()

{

    if($error=Yii::app()->errorHandler->error)

    {

        if(Yii::app()->request->isAjaxRequest)

            echo $error['message'];

        elseif($error['code']!=99 || YII_DEBUG)

            $this->render('error', $error);

    }

}



Thanks Mike,

I see, but still a bit clumsy compared to the CHttpExceptions. I think I’ll better log the error response and only display that an error occured so nobody has to change any system files in order to use my extension

Thanks,

Haensel

But you don’t have to change a system file for it (at least if by system file you mean a framework file). It’s a change in the default SiteController.php.

Ah, you got me. A new Yii lesson learned ;)

Thanks a lot!

Hannes/Haensel

I’d suggest rolling a custom Exception that you can use to do your handling in the SiteController as Mike suggested. Perhaps you can catch it in other ways in the config, I’m not sure to be honest. i.e.




class ActiveResourceException extends CException

{

    const HOST_CONNECT_FAIL = 1;

    const RECORD_NOT_FOUND = 2;

    ...

}



Also a valid option. I think, then you should configure your custom errorHandler component (extending CErrorHandler) and override handleException(). Inside that you then can take special care of your custom exception type.

hello mike,

i am new to yii can you please elaborate this customize exception little more.

becaus i trd that you written in this forum but cant get output.

thanks in advance.

What exactly did you try? And can you explain how you want it to work?