From: Peter Rabbitson Date: Thu, 4 Jul 2013 07:02:18 +0000 (+0200) Subject: Clarify exception on unexpected try() arguments X-Git-Tag: Try-Tiny-0.13~6^2~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=4c5b99d6c581e1cf8d3cc9b0ed80d1effc670c92;p=p5sagit%2FTry-Tiny.git Clarify exception on unexpected try() arguments --- diff --git a/Changes b/Changes index 0b1704a..e45b95e 100644 --- a/Changes +++ b/Changes @@ -4,6 +4,7 @@ - fix (fsvo) broken URLs (RT#55659) - proper exception on erroneous usage of bare catch/finally (RT#81070) - proper exception on erroneous use of multiple catch{} blocks + - clarify exception occuring on unterminated try block (RT#75712) 0.12 - doc fixes diff --git a/lib/Try/Tiny.pm b/lib/Try/Tiny.pm index 3056c4c..f36fe2e 100644 --- a/lib/Try/Tiny.pm +++ b/lib/Try/Tiny.pm @@ -28,18 +28,19 @@ sub try (&;@) { # find labeled blocks in the argument list. # catch and finally tag the blocks by blessing a scalar reference to them. foreach my $code_ref (@code_refs) { - next unless $code_ref; - my $ref = ref($code_ref); - - if ( $ref eq 'Try::Tiny::Catch' ) { + if ( ref($code_ref) eq 'Try::Tiny::Catch' ) { croak 'A try() may not be followed by multiple catch() blocks' if $catch; $catch = ${$code_ref}; - } elsif ( $ref eq 'Try::Tiny::Finally' ) { + } elsif ( ref($code_ref) eq 'Try::Tiny::Finally' ) { push @finally, ${$code_ref}; } else { - confess("Unknown code ref type given '${ref}'. Check your usage & try again"); + croak( + 'try() encountered an unexpected argument (' + . ( defined $code_ref ? $code_ref : 'undef' ) + . ') - perhaps a missing semi-colon before or' + ); } } diff --git a/t/erroneous_usage.t b/t/erroneous_usage.t index c447c61..b64da61 100644 --- a/t/erroneous_usage.t +++ b/t/erroneous_usage.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 5; +use Test::More tests => 6; use Try::Tiny; sub _eval { @@ -46,3 +46,9 @@ throws_ok { try { 1 } catch { 2 } catch { 3 } finally { 4 } finally { 5 } } qr/\QA try() may not be followed by multiple catch() blocks/, 'Multi-catch detected'; + +throws_ok { + try { 1 } catch { 2 } + do { 2 } +} qr/\Qtry() encountered an unexpected argument (2) - perhaps a missing semi-colon before or at/, + 'Unterminated try detected';