diff --git a/modules/Bio/EnsEMBL/DBSQL/DBConnection.pm b/modules/Bio/EnsEMBL/DBSQL/DBConnection.pm index 18bb97e4f9ee6ff2feb91a2ebf56d92842c26b58..82ce05b230c6616b56bc2c6e5977e1f58da8b6fb 100644 --- a/modules/Bio/EnsEMBL/DBSQL/DBConnection.pm +++ b/modules/Bio/EnsEMBL/DBSQL/DBConnection.pm @@ -314,14 +314,15 @@ sub connect { { 'RaiseError' => 1 } ); }; } + my $error = $@; - if ( !$dbh || $@ || !$dbh->ping() ) { + if ( !$dbh || $error || !$dbh->ping() ) { warn( "Could not connect to database " . $self->dbname() . " as user " . $self->username() . " using [$dsn] as a locator:\n" - . $DBI::errstr ); + . $error ); $self->connected(0); @@ -330,7 +331,7 @@ sub connect { . " as user " . $self->username() . " using [$dsn] as a locator:\n" - . $DBI::errstr ); + . $error ); } $self->db_handle($dbh); diff --git a/modules/t/dbConnection.t b/modules/t/dbConnection.t index 8787a3aa20294efbb350239419820b1d05148335..db387b5a4b5b16fb29995f8de34206a2b16a4c93 100644 --- a/modules/t/dbConnection.t +++ b/modules/t/dbConnection.t @@ -2,6 +2,7 @@ use strict; use warnings; use Test::More; +use Test::Exception; use Test::MockObject::Extends; use Time::HiRes qw/usleep/; @@ -216,6 +217,31 @@ my $dbc_copy = mock_object($dbc); $dbc_copy->__is_called('reconnect', 1, "reconnect() as we had gone beyond our normal timeout interval"); } +# Test error reporting when we connect using a bogus driver +{ + throws_ok { + my $dbc = Bio::EnsEMBL::DBSQL::DBConnection->new( + -HOST => '127.0.0.1', -PORT => 3306, -USER => 'usr', -DRIVER => 'bogusdriver' + ); + local $SIG{__WARN__} = sub { + #swallow the warning. we get it raised via an error anyway + }; + $dbc->db_handle; + } qr/install_driver.+failed/, 'Checking for an error message from DBI detailing missing driver problems'; + + if($db->dbc->driver() eq 'mysql') { + throws_ok { + my $dbc = Bio::EnsEMBL::DBSQL::DBConnection->new( + -HOST => $db->dbc->host, -PORT => $db->dbc->port, -USER => 'arandomuser', -DRIVER => 'mysql' + ); + local $SIG{__WARN__} = sub { + #swallow the warning. we get it raised via an error anyway + }; + $dbc->db_handle; + } qr/Access denied for user/, 'Checking we raise an error about a bogus connection details'; + } +} + done_testing(); 1;