[Catalyst] Custom finalize_error

Jason Kohles email at jasonkohles.com
Tue Apr 3 15:10:44 GMT 2007


On Apr 2, 2007, at 5:39 PM, Jim Spath wrote:

> Jim Spath wrote:
>> Jason Kohles wrote:
>>> On Apr 2, 2007, at 4:13 PM, Jim Spath wrote:
>>>
>>>> I wanted to get rid of the "Please come back later" error page,  
>>>> so I defined my own finalize_error() in MyApp.pm as mentioned in  
>>>> this post:
>>>>
>>>> http://www.mail-archive.com/catalyst@lists.rawmode.org/ 
>>>> msg04542.html
>>>>
>>>> It looks like:
>>>>
>>>> sub finalize_error {
>>>>   my ($self, $c) = @_;
>>>>
>>>>   $c->stash->{'template'} = 'error.tt2';
>>>>
>>>> }
>>>>
>>>> The "Please come back later" page no longer appears, but now the  
>>>> page is simply blank.  I've double checked to make sure the  
>>>> error.tt2 template is accessible via Catalyst and it is, so I'm  
>>>> unsure where to go from here.
>>>>
>>> finalize_error is called long after the view processing has  
>>> already been done, if you want to use a template to put a page  
>>> there, you have to build your own Template object and process the  
>>> template yourself.
>> Ah.  Great, thanks!  I got it to work with the following code:
>> sub finalize_error {
>>   my ($c) = @_;
>>   $c->response->content_type('text/html; charset=utf-8');
>>   $c->response->body($c->view('Website')->render($c, 'error.tt2'));
>>   $c->response->status(500);
>> }
>
> As an aside, what are the advantages/disadvantages of defining my  
> own finalize_error() instead of handling errors in end() as  
> described in the Catalyst Cookbook:
>
> http://search.cpan.org/dist/Catalyst-Manual/lib/Catalyst/Manual/ 
> Cookbook.pod#Delivering_a_Custom_Error_Page
>

Dealing with errors in finalize_end allows you a chance to get at  
errors you might not otherwise have been able to handle (such as  
dispatch failures that prevent your end method from being called,  
problems in the end method itself, problems in the View, etc).  In  
your case you negate some of this benefit by using the view to render  
your error page, which means your error handler can't handle any  
problems that may occur with the view.

-- 
Jason Kohles
email at jasonkohles.com
http://www.jasonkohles.com/
"A witty saying proves nothing."  -- Voltaire





More information about the Catalyst mailing list