Embedding error message without changing layout

Hi,

how can I embed thrown Exception into themed main layout?

As I understand, thrown exception will not render main layouts, right?

Normal page has following processing workflow:

  1. protected/views/cms/my-existing-view.php

  2. protected/views/layouts/column2.php

  3. protected/views/layouts/main.php

However, Exceptions will only process:

  1. protected/views/cms/error-page.php

  2. protected/views/layouts/column2.php

Is there any way to embed thrown exception into nice layout from file

[protected/views/layouts/main.php]

with header code 404?

I would like to avoid maintaining duplicate layout files - one for normal files and one for exceptions.

Thank you in advance for any advices.

PS:

I have read guide on handling exceptions, but this did not help much,

since I know how to configure custom exceptions handling, but I would like to have it regularly embedded into

common main layout file. It seems to me that since 1.1.5 there is new parameter $exit in CApplication::end($status=0, $exit=true), which could solve the issue by setting it to $exit=false to continue rendering, but I dont know how can I control it…

Cheers

Lubos

In order to solve this issue, i have created a view specifically created to render errors, and then (due to the ajaxed nature of my cms) call its rendering by renderPartial with its return parameter set to true. Then I flush its result and end the response.

Hope this helps you.

Hi Antonio,

it looks like not easy thing to do:-)

Well, ajax might be solution, but its not quite straightforward built-in solution as I was hoping for…

I may give a try to your solution, if it does not involve high maintenance costs in long term.

Thanx a lot & cheers

Lubos

Hi there,

Imagine it is not ajax… ok? Imagine that, in your rendered view, you check for specific value

if($error != false)

// display error

Then you could do try and catch and if catch set the $error value to the error message you wish to display. At the end of the action you render the file providing the $error variable with it. I don’t think is a problem with it.

I am developing quite a big application at the moment, and I have inserted a function within the Controller component from where all controllers extend from. It actually formats the error message and returns to the caller. How hard is to maintain that function in the long term?

Here an example:

public function notify($message = ‘’, $type = ‘success’, $return = false){

return $this->renderPartial(‘application.views.global._message’,array(‘type’=>$type,‘message’=>$message), $return );

}

The _message.php is the view of the error message and it receives two types of variables. Here is the view:





<div class="notification <?php echo $type;?> png_bg">

<a href="#" class="close">

<img src="<?php echo Yii::app()->request->baseUrl;?>/images/admin/icons/cross_grey_small.png" title="Close this notification" alt="close" />

</a>

<div><?php echo $message;?></div>

</div>




Anyway, thats my input.

Cheers

I am not the only one thinking like this :)

http://www.yiiframework.com/wiki/96/how-to-use-global-controller-independed-view-for-error-handling/

Best