Grokbase Groups Perl dbd-pg May 2009
FAQ
Hi all;

I would like to add error handing which looks at why DBI->connect()
call using DBD::Pg fails and handle it appropriately. For example, if
the authentication fails, I would like to handle that one way, and the
db specified is not found, I would like to handle that a different
way.

After reviewing the docs, I don;t see a reasonable way to do this.
Can anyone point me in the right direction?

Best WIshes,
Chris Travers

Search Discussions

  • Greg Sabino Mullane at Jun 9, 2009 at 12:36 pm

    I would like to add error handing which looks at why DBI->connect()
    call using DBD::Pg fails and handle it appropriately. For example, if
    the authentication fails, I would like to handle that one way, and the
    db specified is not found, I would like to handle that a different
    way.
    Best you can do is look at the error string. You can't rely on $DBI::state,
    alas, as the SQL spec doesn't specify SQLSTATE codes at that level of
    granularity. But as long as you keep your language settings constant,
    you should be able to parse the strings returned. Something like this:

    my $DSN = 'DBI:Pg:dbname=greg;port=5432';

    my $dbh = DBI->connect($DSN, 'greg', '', {AutoCommit=>0,RaiseError=>0,PrintError=>0});
    ## RaiseError=>0 is dangerous unless we check right away
    if ($DBI::err) {
    print "Got errstr as $DBI::errstr\n";
    print "Got state as $DBI::state\n";
    if ($DBI::errstr =~ /database "(.+)" does not exist/) {
    &handle_no_database($1);
    }
    elsif ($DBI::errstr =~ /role "(.+)" does not exist/) {
    &handle_bad_user($1);
    }
    else {
    die "Connection failed: $DBI::errstr\n";
    }
    }
    $dbh->{RaiseError} = 1;
    print "Connected!\n";

    - --
    Greg Sabino Mullane greg@turnstep.com
    End Point Corporation
    PGP Key: 0x14964AC8 200906090835
    http://biglumber.com/x/web?pk=2529DF6AB8F79407E94445B4BC9B906714964AC8

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupdbd-pg @
categoriesperl
postedMay 29, '09 at 6:55p
activeJun 9, '09 at 12:36p
posts2
users2
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase