From b63e399f36d1c16c9ef09b8566e2b5210e319513 Mon Sep 17 00:00:00 2001 From: Andrew Yates <ayates@ebi.ac.uk> Date: Tue, 1 Oct 2013 12:37:46 +0000 Subject: [PATCH] [ENSCORESW-665]. Making sure we report the correct DBI error. It seems that $DBI::errstr has a very strange life. We can get the error message recived by incorrect connections or bad drivers (DBD::mysql is missing) when we just warn the error. However concatenating it into another string makes the message disapear. Even assignment and then concat will not work. Instead we have switched to using the $@. This has the error in it. --- modules/Bio/EnsEMBL/DBSQL/DBConnection.pm | 7 +++--- modules/t/dbConnection.t | 26 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/modules/Bio/EnsEMBL/DBSQL/DBConnection.pm b/modules/Bio/EnsEMBL/DBSQL/DBConnection.pm index 18bb97e4f9..82ce05b230 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 8787a3aa20..db387b5a4b 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; -- GitLab