Re: [Catalyst] Trapping exceptions in Catalyst.pm

2010-08-04 Thread Ton Voon


On 2 Aug 2010, at 07:49, Bill Moseley wrote:


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 = pop( @{ $c-stack } );

if ( my $error = $@ ) {

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 = $@; ...}; style.


I did a bit of work in DBIx::Class where I changed all the eval  
statements to using Try::Tiny instead. Not sure if it got into trunk  
yet. A similar conversion here would work.


Ton


___
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/


Re: [Catalyst] Trapping exceptions in Catalyst.pm

2010-08-04 Thread Tomas Doran


On 4 Aug 2010, at 10:06, Ton Voon wrote:
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 = $@; ...}; style.


I did a bit of work in DBIx::Class where I changed all the eval  
statements to using Try::Tiny instead. Not sure if it got into trunk  
yet. A similar conversion here would work.


Branch to do that (Try::Tiny) entirely welcome, we already depend on  
it (via Moose).


Cheers
t0m


___
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/


[Catalyst] Trapping exceptions in Catalyst.pm

2010-08-02 Thread Bill Moseley
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 = pop( @{ $c-stack } );

if ( my $error = $@ ) {

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 = $@; ...}; style.

-- 
Bill Moseley
mose...@hank.org
___
List: Catalyst@lists.scsys.co.uk
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
Dev site: http://dev.catalyst.perl.org/