From: Peter Rabbitson Date: Tue, 28 Oct 2008 12:32:52 +0000 (+0000) Subject: Fix an obscure bug in the DBI exception handler, clobbering the exception contents... X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9bf06dc034dc367be87d5f69340515de758edebd;p=dbsrgits%2FDBIx-Class-Historic.git Fix an obscure bug in the DBI exception handler, clobbering the exception contents when the same $dbh is used outside of dbic code --- diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index 4d52630..35c31a3 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -882,7 +882,12 @@ sub _connect { my $weak_self = $self; weaken($weak_self); $dbh->{HandleError} = sub { - $weak_self->throw_exception("DBI Exception: $_[0]") + if ($weak_self) { + $weak_self->throw_exception("DBI Exception: $_[0]"); + } + else { + croak ("DBI Exception: $_[0]"); + } }; $dbh->{ShowErrorStatement} = 1; $dbh->{RaiseError} = 1; diff --git a/t/34exception_action.t b/t/34exception_action.t index 8fd70ba..173e435 100644 --- a/t/34exception_action.t +++ b/t/34exception_action.t @@ -5,7 +5,7 @@ use Test::More; use lib qw(t/lib); use DBICTest; -plan tests => 8; +plan tests => 9; # Set up the "usual" sqlite for DBICTest my $schema = DBICTest->init_schema; @@ -68,3 +68,15 @@ like($@, qr/DBICTest::Exception is handling this: $ex_regex/); # While we're at it, lets throw a custom exception through Storage::DBI eval { $schema->storage->throw_exception('floob') }; like($@, qr/DBICTest::Exception is handling this: floob/); + + +# This usage is a bit unusual but it was actually seen in the wild +eval { + + my $dbh = $schema->storage->dbh; + undef $schema; + + $dbh->do ('glaring_syntax_error;'); +}; +like($@, qr/DBI Exception.+do failed/, 'Exception thrown even after $storage is destroyed'); +