[Catalyst] Trapping exceptions in Catalyst.pm

Bill Moseley moseley at hank.org
Mon Aug 2 06:49:28 GMT 2010


In execute() there's this code:

    eval { $c->state( $code->execute( $class, $c, @{ $c->req->args } ) || 0
) };

    $c->_stats_finish_execute( $stats_info ) if $c->use_stats and
$stats_info;

    my $last =3D pop( @{ $c->stack } );

    if ( my $error =3D $@ ) {

The problem is that it's possible for the eval to fail but $@ is not set.
 An example is where Locale::Maketext localizes $@ so that exceptions come
back with $@ undefined (for some odd reason).

In general, it's better to test the return value from eval directly instead
of depend on $@.  Something like:

my $has_exception;
eval { $c->state( $code->execute( $class, $c, @{ $c->req->args } ) || 0 );
1; } || $has_exception++;
...
if ( $has_exception ) {

Or use the "eval {....; 1 } || do { my $msg =3D $@; ...};" style.

-- =

Bill Moseley
moseley at hank.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20100801/5fc28=
1e6/attachment.htm


More information about the Catalyst mailing list