Re: Problems with database handle attributes in error handle



On Mon, Mar 27, 2006 at 02:04:30PM +0100, Martin J. Evans wrote:
Hi,

I have my own error handler in which I'd like to log as much detail about the
error as possible. In this handler I do something like:

sub _error_handler {
my ($msg, $h, $method_ret) = @_;
my $dbh = $h;
$dbh = $h->{Database} if ($h->{Type} eq "st");
print "DB: ". $dbh->{Name}.
", Username: ". $dbh->{Username};
print "Error: (handle type: ". $h->{Type}. ")" .
"(SQL:" . $h->{Statement} . ") (msg:".
$h->errstr . ")");
print Carp::longmess("DBI error trap")};
}

(vastly simplified).

This works fine when $h is type "st" and is mostly fine when $h is type "dr"
with one notable exception - when DBI->connect fails as I get:

Can't get DBI::dr=HASH(0x83cbbc4)->{Username}: unrecognised attribute name at
/usr/local/lib/perl5/site_perl/5.8.7/i686-linux/DBI.pm line 1329.

Funnily enough I just fixed that yesterday.

I cannot protect the $dbh->{Username} references by checking for existance
first e.g.
print "Username: " . $dbh->{Username} if (exists($dbh->{Username}));
as I get the same warning message. Is this correct behavior?

Yes. It's a tied hash and the DBI doesn't provide a useful EXISTS method.

I found:

Yeap. That's the right spot. Just add in a case for Username.

Tim.
.



Relevant Pages

  • Re: DBD::Sybase message suppression....
    ... > beautifully, thanks to the authors of DBI and the DBDs - Thank you, ... > These also work just fine, but print message regardless of whether I ... You can trap these by using an error handler. ...
    (perl.dbi.users)
  • Setting Error Handler
    ... I'm trying to implement a mechanism where a DBI program can detect its DB ... Basically I have set up a custom error handler that checks for certain ... If however the error occurs a second time, the custom error handler doesn't ... Can anyone tell me if this is a bug with the old version of DBI we are ...
    (perl.dbi.users)
  • RE: Setting Error Handler
    ... Subject: Setting Error Handler ... I'm trying to implement a mechanism where a DBI program can detect its DB ... Basically I have set up a custom error handler that checks for certain ...
    (perl.dbi.users)