From: Konstantin A. Pustovalov Date: Mon, 21 Jan 2013 20:40:58 +0000 (+0400) Subject: Add tests and fix for exceptions which stringify to empty string handled by exception... X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=24692938f6ce7e2fd736d7479c33096e614db978;p=dbsrgits%2FDBIx-Class-Historic.git Add tests and fix for exceptions which stringify to empty string handled by exception_action --- diff --git a/lib/DBIx/Class/Storage/BlockRunner.pm b/lib/DBIx/Class/Storage/BlockRunner.pm index 8b9ea26..319914c 100644 --- a/lib/DBIx/Class/Storage/BlockRunner.pm +++ b/lib/DBIx/Class/Storage/BlockRunner.pm @@ -145,7 +145,7 @@ sub _run { my $storage = $weakself->storage; my $cur_depth = $storage->transaction_depth; - if (defined $txn_init_depth and $run_err eq '') { + if (defined $txn_init_depth and not $run_err) { my $delta_txn = (1 + $txn_init_depth) - $cur_depth; if ($delta_txn) { @@ -163,7 +163,7 @@ sub _run { } # something above threw an error (could be the begin, the code or the commit) - if ($run_err ne '') { + if ($run_err) { # attempt a rollback if we did begin in the first place if ($txn_begin_ok) { diff --git a/t/33exception_wrap.t b/t/33exception_wrap.t index fdee230..68571d3 100644 --- a/t/33exception_wrap.t +++ b/t/33exception_wrap.t @@ -23,4 +23,17 @@ is_deeply ( 'Exception-arrayref contents preserved', ); +# Exception class which stringifies to empty string +{ + package DBICTest::ExceptionEmptyString; + use overload bool => sub { 1 }, '""' => sub { "" }, fallback => 1; + sub new { bless {}, shift; } +} + +dies_ok (sub { + $schema->txn_do (sub { die DBICTest::ExceptionEmptyString->new }); +}, 'Exception-empty string object thrown'); + +isa_ok $@, 'DBICTest::ExceptionEmptyString', "Exception-empty string"; + done_testing; diff --git a/t/34exception_action.t b/t/34exception_action.t index 6de03f0..b463ce8 100644 --- a/t/34exception_action.t +++ b/t/34exception_action.t @@ -28,8 +28,18 @@ throws_ok { $e->rethrow } isa_ok( $@, 'DBIx::Class::Exception' ); # Now lets rethrow via exception_action -$schema->exception_action(sub { die @_ }); -throws_ok \&$throw, $ex_regex; +{ + my $handler_execution_counter = 0; + + $schema->exception_action(sub { + $handler_execution_counter++; + like $_[0], $ex_regex, "Exception is precisely passed to exception_action"; + die @_ + }); + + throws_ok \&$throw, $ex_regex; + is $handler_execution_counter, 1, "exception_action handler executed exactly once"; +} # # This should have never worked!!! @@ -80,4 +90,20 @@ throws_ok \&$throw, throws_ok { $schema->storage->throw_exception('floob') } qr/DBICTest::Exception is handling this: floob/; +# Exception class which stringifies to empty string +{ + package DBICTest::ExceptionEmptyString; + use overload bool => sub { 1 }, '""' => sub { "" }, fallback => 1; + sub new { bless {}, shift; } +} + +# Try the exception-empty string class +$schema->exception_action(sub { die DBICTest::ExceptionEmptyString->new }); +dies_ok \&$throw; + +my $throw_x_empty_string = sub { $schema->txn_do (sub { die DBICTest::ExceptionEmptyString->new }); }; +$schema->exception_action(sub { die @_ }); +dies_ok \&$throw_x_empty_string, 'exception_action handles exception object-empty string'; +isa_ok $@, 'DBICTest::ExceptionEmptyString', "exception rethrown by exception_action"; + done_testing;