mnlight has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to capture a specific sybase error then report that the error occured and the script continued and completed successfully.

Below is code that loads transaction files to a refreshed database. On occasion the files get out of sequence (Sybase ERROR 4305)and the script fails. I want this error to be captured break the foreach loop bring the database on line, leave the subroutine and then continue to the next step.

foreach my $tran_set ( @$trans ) { my $i = 0; foreach my $stripe ( @$tran_set ) { my $localfile = $stripe; $localfile =~ s(^.*/)($local_root); $stripe = $localfile; if ( $i == 0 ) { $sql = "load tran $db from \'$stripe\'\n"; } else { $sql .= "stripe on \'$stripe\'\n"; } $i++; } $$dbh->do($sql) or $app->log($FATAL, "Can't do sql statement [ $sql ]: $DBI::errst +r"); foreach my $stripe ( @$tran_set ) { rmtree($stripe); } } $sql = "online database $db"; $$dbh->do($sql) or $app->log($FATAL, "Can't do sql statement [ $sql ]: $DBI::errst +r"); }

Replies are listed 'Best First'.
Re: capture sybase error
by jfroebe (Parson) on Nov 23, 2005 at 19:14 UTC

    Hi,

    You need to set up the syb_err_handler. Take a look at the DBD::Sybase documentation.

    From the man page:

    sub err_handler { my($err, $sev, $state, $line, $server, $proc, $msg, $sql, $err_type) = @_; my @msg = (); if($err_type eq 'server') { push @msg, ('', 'Server message', sprintf('Message number: %ld, Severity %ld, State %ld, Lin +e %ld', $err,$sev,$state,$line), (defined($server) ? "Server '$server' " : '') . (defined($proc) ? "Procedure '$proc'" : ''), "Message String:$msg"); } else { push @msg, ('', 'Open Client Message:', sprintf('Message number: SEVERITY = (%ld) NUMBER = (%ld)', $sev, $err), "Message String: $msg"); } print STDERR join("\n",@msg); return 0; ## CS_SUCCEED }

    NOTE - if you set the error handler in the DBI->connect() call like this

    $dbh = DBI->connect('dbi:Sybase:server=troll', 'sa', '', { syb_err_handler => \&err_handler });

    Jason L. Froebe

    Team Sybase member

    No one has seen what you have seen, and until that happens, we're all going to think that you're nuts. - Jack O'Neil, Stargate SG-1

Re: capture sybase error
by mpeppler (Vicar) on Nov 23, 2005 at 19:32 UTC
    I guess that it's the $app->log($FATAL...) bit that causes the script to exit.

    You could probably do something like this:

    foreach ....( ... ) { my $rc = $dbh->do($sql); if(!$rc) { if($dbh->err == 4305) { # print a warning here? last; } else { $app->log(...); } } } ...
    Michael