Displaying Custom Message Instead Of

I have a problem with setting custom message instead of something like this:

"Error 500 : <h1>CDbException</h1>

… and stack trace"

I want to set user-friendly message (e.g. “You can’t delete this object. It is in relation with other objects”) when someone want delete a record which is in relation with other.

I was trying to do something like this:




public function actionDelete($id)

	{

            //

                try

                {

                $this->loadModel($id)->delete();

                

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser

		if(!isset($_GET['ajax']))

			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));

                }

                catch(CDbException $e)

                {                    

                    throw new CDbException('Message');

                }

                    

	}




But this is not working.

I also tried a way with setFlash but it also is not working.

Hi Giewont90

you have to write the file error500.php in

protected/views/system or to your theme /themes/<yourtheme>/views/system

Check writing the code “echo nl2br(CHtml::encode($data[‘message’]));” inside of this file.

Let me know if it works! :)

Not working :confused:

Please read the below document

http://www.yiiframework.com/doc/guide/1.1/en/topics.error

I will try it later

Ok, I solved it in antoher way.

Now I have:




public function actionDelete($id)

        {

            //

                try

                {

                $this->loadModel($id)->delete();

                

                // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser

                if(!isset($_GET['ajax']))

                        $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));

                }

                catch(Exception $e)

                {                    

                    throw new CHttpException('','Message');

                }

                    

        }



And works well :)

Hi Giewont90

both error 404 , 500 redirect internaly

to controller-action site/error so, you have to change the protected/views/site/error.php as you desire

note: This works in production mode - YII_DEBUG=false