Skip to content
Snippets Groups Projects
Commit b63e399f authored by Andy Yates's avatar Andy Yates
Browse files

[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.
parent de163a59
No related branches found
No related tags found
No related merge requests found
......@@ -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);
......
......@@ -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;
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment