Cannot find DBI error
I am writing a Perl script and I see no DBI errors no matter what I try. I've tried this:
use DBI;
$db = DBI->connect("dbi:ODBC:Driver={SQL Server};Server=localhost;DATABASE=nodepoint;UID=sa;PWD=test;") or print "Something happened.";
and this:
use DBI;
eval
{
$db = DBI->connect("dbi:ODBC:Driver={SQL Server};Server=localhost;DATABASE=nodepoint;UID=sa;PWD=test;");
};
if ($@) { print "Something happened."; }
Both fail to catch the error and instead I get this on screen:
DBI connect('Driver={SQL Server};Server=localhost;DATABASE=nodepoint;UID=sa;PWD=test','',...) failed: [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server does not exist or access denied. (SQL-08001) [state was 08001 now 01000]
[Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionOpen (Connect()). (SQL-01000) at C:\dev\test.pl line 5.
This is a big problem because when used in IIS it throws 500.2 Bad Gateway
when it sees an error. I need to catch it so that I can display the correct message.
+3
source to share
1 answer
Default error handling:
RaiseError => 0
PrintError => 1
PrintWarn => 0
You want to transfer PrintError => 0
to connect
.
If you prefer to check for errors:
my $dbh = DBI->connect($dsn, $user, $passwd, {
RaiseError => 0,
PrintError => 0,
});
if (!$dbh) {
die($DBI::errstr);
}
If you prefer to have exceptions:
my $dbh = eval {
DBI->connect($dsn, $user, $passwd, {
RaiseError => 1,
PrintError => 0,
})
};
if (!$dbh) {
die($@);
}
+6
source to share