Perl Dblib Error Handling
Contents |
Q&A Tutorials Poetry RecentThreads NewestNodes Donate What'sNew on Jun 25, 2004 at 09:13UTC ( #369579=perlquestion: print w/replies, xml ) Need Help?? Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: Hi Monks, I am using the Sybase::Dblib module to run my sql query. perl dbi execute error handling I have one stored proc which takes ID as argument and returns the perl dbi execute return value record associated with that ID. In case there is no record for that ID it prints Server Message: Number 20000, Severity perl dbi connect error handling 16 Server 'NYTIBV3T008', Procedure 'db2pwGetTCMInfo_test', Line 10: No TCM number found for this PW number (1 row affected) (return status = -1) Now when i call this from the URL it prints everything on the perl dbi handleerror browser and i dont want that error messages to be printed on the browser. Is there any way for not allowing that code to be printed on the browser and i dont have permission to change the stored proc. I am using this code. $tcm->dbcmd("EXEC db2pwGetTCMInfo $data{row_id}"); $tcm->dbsqlexec || die "Error in dbsqlexec"; $tcm->dbresults; and dbsqlexec is the culprict which prints everything on the screen and i can't get away
Perl Dbi Errstr
from calling this function. Thanks. Comment on Error Handeling in Sybase::Dblib Replies are listed 'Best First'. Re: Error Handeling in Sybase::Dblib by derby (Abbot) on Jun 25, 2004 at 12:17UTC Take out the || die logic and setup an error handler to trap the error (you have to decide how you want to handle the error). Or, scap the dbcmd/dbsqlexec/dbresults and use the nsql method. It's all there in the docs. -derby [reply][d/l] Re: Error Handeling in Sybase::Dblib by mpeppler (Vicar) on Jun 25, 2004 at 12:36UTC Error messages are printed from the "message" handler in DBlib. For Sybase::DBlib you create a subroutine that will handle any messages, and then install it with dbmsghandle(\&my_handler_sub). For example: sub message_handler { my ($db, $message, $state, $severity, $text, $server, $procedure, +$line) = @_; # Don't display 'informational' messages: if ($severity > 10) { print STDERR ("Sybase message ", $message, ", Severity ", $severit +y, ", state ", $state); print STDERR ("\nServer `", $server, "'") if defined ($server); print STDERR ("\nProcedure `", $procedure, "'") if defined ($proce +dure); print STDERR ("\nLine ", $line) if defined ($line); print STDERR ("\n ", $text, "\n\n"); # &dbstrcpy returns the command buffer. if(defined($db)) { my ($lineno, $cmdbuff) = (1, undef); my $row; $cmdbuff = &S
the answer generally runs along the lines of "Why aren't you performing error checking?" Sure enough, nine out of ten times when error
Perl Dbi Try Catch
checking is added, the exact error message appears and the cause for try catch in perl error is obvious. 4.5.1. Automatic Versus Manual Error Checking Early versions of the DBI required programmers to perform dbi error fatal their own error checking, in a traditional way similar to the examples listed earlier for connecting to a database. Each method that returned some sort of status indicator as to http://www.perlmonks.org/?node_id=369579 its success or failure should have been followed by an error condition checking statement. This is an excellent, slightly C-esque way of programming, but it quickly gets to be tiresome, and the temptation to skip the error checking grows. The DBI now has a far more straightforward error-handling capability in the style of exception s. That is, when DBI internally detects http://docstore.mik.ua/orelly/linux/dbi/ch04_05.htm that an error has occurred after a DBI method call, it can automatically either warn() or die() with an appropriate message. This shifts the onus of error checking away from the programmer and onto DBI itself, which does the job in the reliable and tireless way that you'd expect. Manual error checking still has a place in some applications where failures are expected and common. For example, should a database connection attempt fail, your program can detect the error, sleep for five minutes, and automatically re-attempt a connection. With automatic error checking, your program will exit, telling you only that the connection attempt failed. DBI allows mixing and matching of error-checking styles by allowing you to selectively enable and disable automatic error checking on a per-handle basis. 4.5.1.1. Manual error checking Of course, the DBI still allows you to manually error check your programs and the execution of DBI methods. This form of error checking is more akin to classic C and Perl programming, where each important statement is checked to ensure that it has executed successfully, allowing the program to
A pointer to the user function that will be called whenever DB-Library determines that an error has occurred. DB-Library calls this function with six parameters: Table 2-16: Error http://infocenter.sybase.com/help/topic/com.sybase.help.ocs_12.5.1.dblib/html/dblib/X12458.htm handler parameters Parameter Meaning dbproc The affected DBPROCESS. If there is no DBPROCESS associated with this error, this parameter will be NULL. severity The severity of the error (datatype http://www.verycomputer.com/160_adc31d6caf179ae4_1.htm int). Error severities are defined in syberror.h. dberr The identifying number of the error (datatype int). Error numbers are defined in sybdb.h. oserr The operating-system-specific error number that describes perl dbi the cause of the error (datatype int). If there is no relevant operating system error, the value of this parameter will be DBNOERR. dberrstr A printable description of dberr (datatype char *). oserrstr A printable description of oserr (datatype char *). The error handler must return one of the following four values, directing DB-Library to perform particular actions: perl dbi execute Table 2-17: Error handler returns Return Action INT_EXIT Print an error message and abort the program. DB-Library will also return an error indication to the operating system. (Note to UNIX programmers: DB-Library will not leave a core file. INT_CANCEL Return FAIL from the DB-Library routine that caused the error. Returning INT_CANCEL on timeout errors will kill the dbproc. INT_TIMEOUT Cancel the operation that caused the error but leave the dbproc in working condition. This return value is meaningful only for timeout errors (SYBETIME). In any other case, this value will be considered an error, and will be treated as an INT_EXIT. INT_CONTINUE Continue to wait for one additional timeout period. At the end of that period, call the error handler again. This return value is meaningful only for timeout errors (SYBETIME). In any other case, this value will be considered an error, and will be treated as an INT_EXIT. If the error handler returns any value besides these four, the program will abort. Error handlers on the Windows platform must be declare
of my projects. Most of it works except for error handling. I just simply cannot get this simple thing to work! Here is an example straight from the manpage: $dbh->nsql("select * from really_huge_table","HASH",\&parse_array); if ( $DB_ERROR ) { # error handling code goes here, perhaps: die "Unable to get stuff from really_huge_table: $DB_ERROR\n"; } It does a query using the nsql method and supposedly the $DB_ERROR is set if an error occurs. But in my project, nothing ever gets set to the $DB_ERROR variable regardless of error. I've even intentionally put an error in my SQL statement as a test and the $DB_ERROR does not get set so I can never handle my database errors. What is the best way to catch errors using the Sybase::DBlib module? I'm wary of this $DB_ERROR method (it seems like a hack -- what if I'm already using $DB_ERROR?). Does Sybase::DBlib throw exceptions that can be caught in an eval { } block? Exceptions seem to be the clean way to do this. Thanks in advance for any suggestions! Top Perl Help With Sybase::DBlib and $DB_ERROR by Michael Pepple » Thu, 21 Feb 2002 04:58:11 > Hi, I'm using the Perl module Sybase::DBlib for database access for one > of my projects. Most of it works except for error handling. I just > simply cannot get this simple thing to work! nsql() was donated to sybperl by Phil Moore - AFAIK it works as expected. However, OpenClient error handling is *normally* done via error handlers (i.e. perl level callbacks). You should check out dbmsghandle() and the sybutil.pl file in the sybperl distribution for some examples, and then build from there. On a slightly different tack - if you are writing new applications you should really use Sybase::CTlib rather than Sybase::DBlib - Sybase has not added support for any of the newer features available at the server and client level to DBlib in quite a while. If Sybase::CTlib seems to complicated may I suggest Sybase::Simple? It's available on CPAN, and it will make your life a lot simpler :-) Michae