[Catalyst] Re: Error handling in Template render

David Schmidt davewood at gmx.at
Thu Sep 30 07:02:59 GMT 2010


On Thu, Sep 30, 2010 at 8:27 AM, David Schmidt <davewood at gmx.at> wrote:
> If an exception is thrown it's usually put into the $c->error array.
> When this error happens during rendering this doesn't seem to be the
> case and I am trying to find out what Catalyst is doing.
>
>
> In my example forward('render') causes the body to be filled with an
> error array.
>
>
> This is the body after forward('render')
>
> $VAR1 = bless( [
>                 'undef',
>                 bless( {
>                          'msg' =>
> 'Physio::Schema::Result::Exercises::media(): DBI Exception:
> DBD::Pg::st execute failed: ERROR:  column me.mediatype does not exist
> LINE 1: ..., me.name, me.position, me.file, me.content_type, me.mediaty...
>                                                             ^ [for
> Statement "SELECT me.id, me.exercise_id, me.name, me.position,
> me.file, me.content_type, me.mediatype, me.created, me.updated FROM
> media me WHERE ( me.exercise_id = ? ) ORDER BY position" with
> ParamValues: 1=\'1\'] at
> /home/david/catalyst/Physio/root/templates/exercises/show.tt line 17
> '
>                        }, 'DBIx::Class::Exception' ),
>                 \'
> ...'
>               ], 'Template::Exception' );
>
>
>
>
> How do I handle this kind of error?
>
>
> sub end : Private {
>    my ($self, $c) = @_;
>
>    $c->forward('render');
>
>    # display catalyst error page
>    return if $c->debug;
>    # in production log error and display nice error page
>    if (@{$c->error}) {
>        for my $error (@{$c->error}) {
>            $c->log->error($error);
>        }
>        $c->stash(template => 'error.tt');
>        $c->clear_errors;
>    }
> }
>
> sub render : ActionClass('RenderView') {}
>



After reading the Catalyst::View::TT::render source where I found this

$c->log->debug('The Catalyst::View::TT render() method will start
dying on error in a future release. Unless you are calling the
render() method manually, you probably want the new behaviour, so set
render_die => 1 in config for ' . blessed($self) . '. If you wish to
continue to return the exception rather than throwing it, add
render_die => 0 to your config.') if $c->debug;



'MyApp::View::TT' => {
   render_view => 1,
   ...
},

in MyApp.pm I am getting close to the behaviour I want. I have to
forward to render again after setting the error template though.


sub end : Private {
   my ($self, $c) = @_;

   $c->forward('render');

   # display catalyst error page
   return if $c->debug;

   # in production log error and display nice error page
   if (@{$c->error}) {
       for my $error (@{$c->error}) {
           $c->log->error($error);
       }
       $c->stash(template => 'error.tt');
       $c->clear_errors;
       $c->forward('render'); # trigger rendering
   }
}

sub render : ActionClass('RenderView') {}



More information about the Catalyst mailing list