Subject: | connect() doesnt set errstr when the DB driver is missing |
Date: | Thu, 1 Nov 2018 18:15:19 +0000 |
To: | bug-DBI [...] rt.cpan.org |
From: | Marshall Mills <marshall.mills [...] gmail.com> |
Hey everyone,
DBI->connect() doesn't set $DBI::errstr, at least when the DB driver is missing.
This cost me some time tracing down what I thought was a firewall
issue. But, it just turned out that the mysql driver was missing.
Linux 4.1.12-103.7.3.el7uek.x86_64
Perl v5.16.3
Expected output:
DBI::VERSION 1.642
DBI::errstr: install_driver(uninstalledDriver) failed: Can't locate
DBD/uninstalledDriver.pm in @INC
Failed to correctly connect to the `example` database.
Actual output:
DBI::VERSION 1.642
Failed to correctly connect to the `example` database.
Thank you for your time,
Marshall
------------------------------------------------------------------------------------------
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
print( "DBI::VERSION " . $DBI::VERSION . "\n" );
my $databaseName = 'example';
my $server = 'data.example.com';
my $userName = 'admin';
my $password = 'Example code, please dont judge';
my $dataSource = "DBI:uninstalledDriver:database=$databaseName;host=$server";
my $dbConnection = undef;
# https://metacpan.org/pod/DBI#connect
# DBI->connect will die on a driver installation failure and will only
return undef on a connect failure,
# in which case $DBI::errstr will hold the error message. Use eval if
you need to catch the "install_driver" error.
eval
{
$dbConnection = DBI->connect( $dataSource, $userName, $password,
{RaiseError=>1} );
};
if (defined( $DBI::errstr ))
{
print( "DBI::errstr: " . $DBI::errstr . "\n" );
}
if (!defined( $dbConnection ))
{
print( "Failed to correctly connect to the `$databaseName` database.\n" );
exit 1;
}
if (defined( $dbConnection ))
{
$dbConnection->disconnect() || warn $dbConnection->errstr();
}
exit 0;