Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
There's more than one way to do things
 
PerlMonks  

Capturing error thrown by a database

by puneet.keswani (Novice)
on Aug 01, 2011 at 05:50 UTC ( #917760=perlquestion: print w/ replies, xml ) Need Help??
puneet.keswani has asked for the wisdom of the Perl Monks concerning the following question:

Hi.. I am stuck from quite some days now.. Here s the scenario I have a main perl script and a self designed perl module called common.pm, in the module I have writen 3 subroutenes, 1st to connect to Sybase, 2nd calling a sybase proc with parameters and 3rd disconnecting from Sybase.. in my main perl script I am using this module with the help of ; use common.pm From the main perl script I am calling the subroutenes to connect to database.. &common::sybaseconnection(parameters) Now my problem is that I want to capture the error message thrown by a database say when the password has expired and the connection cannot be made in the perl script so that I can log that message in a log file. The errors thrown by the databse can be seen as ouput on the screen but I am unable to capture those error meaasges in my main perl script.. These are simple error messages which database would throw incase it was supplied with wrong user id password. Please help !!

Comment on Capturing error thrown by a database
Re: Capturing error thrown by a database
by CountZero (Chancellor) on Aug 01, 2011 at 06:20 UTC
    It is difficult to be certain without having seen any of your code, but it looks as if you are printing your error messages, rather than returning them from your subroutine and saving them in a variable for further handling.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      I am actually using "out" parameters to return the error messages and capturing it in the main perl script with the help of func(syb_out_params), but this is for the user defined error messages.. My problem is what about the error messages which will not be handled by the subroutene, take for example "password has expired" while connecting to the database then database connection will not be possible and database will throw its own error.. How to catch these types of errors..
        When the DBI has an error, it is available as the string provided by DBI->errstr. So for example in a wild guess at your subroutine below, if the connection fails (no handle returned), the sub returns the DBI->errstr. Of course your calling program would have to do something with that. PrintError=1 causes warning message to be printed, you may or may not want that. RaiseError=1 is a bit stronger and causes a "die", which you don't want unless you inside of a block eval.

        You can set RaiseError =1 and put statements in an eval{} which in this context would be Perl's equivalent of a try/catch. In the case of failure, again, calling the errstr method of the DBI will give you its error.

        my $dbh; sub connectsybase { my ($var1, $var2,...) = @_; $dbh = DBI->connect($data_source, $username, $auth, {printError => 1} ); return (DBI->errstr) if (!dbh); # fail return ""; # success, NULL string }
Re: Capturing error thrown by a database
by onelesd (Pilgrim) on Aug 01, 2011 at 06:56 UTC
    Take a look at DBI and it's error-related attributes.
    use DBI ; my $dbh = DBI->connect($data_source, $username, $auth); $dbh->{PrintError} = 1 ; $dbh->{RaiseError} = 1 ;
      I have used the attributes but without much luck.. while trying to connect to database, I have also tried wrapping the code within eval but again its not helping. If you want I can paste the code..
        Posting your code would help. You can define a subroutine to handle all of your DBI errors: HandleError.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://917760]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (11)
As of 2014-04-25 08:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (585 votes), past polls