Test Database Connection

I am trying to create a controller action that will test a user-defined database connection. I am calling this controller action with ajax. My problem is that if the database connection fails, it throws an Exception (which is handled by Yii) and the message returned to the user is ugly. Instead, I want to use my own controller action to display a nice message to the user. Is there a way to do this?

Hi

  1. you could modify the protected/views/site/error.php to display what you want

  2. if you want to redirect to another error file on the fly (before error occurs) you could use this


Yii::app()->setComponents(array(

    'errorHandler'=>array(

        'errorAction'=>'/anotherfolder/yourerror'

    )

));



Thanks! Does this work for Ajax? When I try it out, it does not go to the controller action specified by errorAction. It appears that CApplication::displayError() is being called on Ajax requests, which is preventing the controller action from being called. See line 284 in class CErrorHandler Yii v1.1.13.

if you check the default eroor action you will see in ajax request case that only the message return rather that entire layout, so you have to do the same.


public function actionError() {

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

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

                echo $error['message'];

            else

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

        }

    }

That works. Appreciate your help.