Subject: | Try::Tiny catch is not catching exceptions |
Date: | Fri, 28 Jan 2011 09:38:57 +1300 |
To: | bug-DBIx-Connector [...] rt.cpan.org |
From: | Mark Lawrence <nomad [...] null.net> |
According to the docs/SYNOPSIS the following should work:
use DBIx::Connector;
use Try::Tiny qw/ catch /;
my $conn = DBIx::Connector->new( 'dbi:SQLite:dbname=junkxxx' );
$conn->txn(no_ping => sub {
die 'WTF!';
}, catch {
warn "Caught $_";
})
Unfortunately I get:
WTF! at - line 7.
Replacing s/catch/sub/ does the expected thing. I don't know if the
docs or the code are broken... but here is a start on a test:
diff --git a/t/txn.t b/t/txn.t
index dd9a23a..f8210d7 100644
--- a/t/txn.t
+++ b/t/txn.t
@@ -5,6 +5,7 @@ use warnings;
use Test::More tests => 104;
#use Test::More 'no_plan';
use Test::MockModule;
+use Try::Tiny;
my $CLASS;
BEGIN {
@@ -159,6 +160,15 @@ ok $conn->txn(sub {
}), 'Catch and handle another exception';
is $@, 'foo', '$@ still should not be changed';
+# Check Try::Tiny 'catch' method
+ok $conn->txn(sub {
+ die 'WTF!';
+}, catch {
+ like $_, qr/WTF!/, 'Should catch another exception';
+ like shift, qr/WTF!/, 'catch arg should also be the new exception';
+}), 'Catch and handle another exception';
+is $@, 'foo', '$@ still should not be changed';
+
eval { $conn->txn(sub { die 'WTF!' }, catch => sub { die 'OW!' }) };
ok my $e = $@, 'Should catch exception thrown by catch';
like $e, qr/OW!/, 'And it should be the expected exception';
Secondly, and I'm not sure if this is related or not but I'm seeing
this kind of problem in the wild:
$self->conn->run(no_ping => sub {
die "lkjsdf";
}, sub {
warn "Caught no_ping $_";
});
$self->conn->run(ping => sub {
die "lkjsdf";
}, sub {
warn "Caught ping $_";
});
$self->conn->run(fixup => sub {
die "fdsalkj";
}, sub {
warn "Caught fixup $_";
});
...
Caught no_ping lkjsdf at ../sql-db/lib/SQL/DB.pm line 190.
Caught ping lkjsdf at ../sql-db/lib/SQL/DB.pm line 196.
fdsalkj at ../sql-db/lib/SQL/DB.pm line 202.
Haven't managed to reproduce standalone yet. It seems that fixup mode
is not running any kind of catch block if the main subroutine raises an
exception, whereas ping and no_ping modes do.
$DBIx::Connector::VERSION is 0.42.
Mark.
--
Mark Lawrence