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.
I have one stored proc which takes ID as argument and returns the record associated with that ID. In case there is no record for that ID it prints
Server Message: Number 20000, Severity 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 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 from calling this function.
Thanks.

Replies are listed 'Best First'.
Re: Error Handeling in Sybase::Dblib
by derby (Abbot) on Jun 25, 2004 at 12:17 UTC
    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
Re: Error Handeling in Sybase::Dblib
by mpeppler (Vicar) on Jun 25, 2004 at 12:36 UTC
    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 = &Sybase::DBlib::dbstrcpy($db); foreach $row (split (/\n/, $cmdbuff)) { print STDERR (sprintf ("%5d", $lineno ++), "> ", $row, "\n"); } } } elsif ($message == 0) { print STDERR ($text, "\n"); } return 0; } dbmsghandle(\&message_handler);
    Now when any error (or PRINT statement in SQL) is executed/sent from the server it will be sent to your message handler, and you can control how and where the messages get printed.

    See also the Sybase docs for OpenClient at http://sybooks.sybase.com/onlinebooks/group-sd/sdg1251e/dblib, and of course the Sybase::DBlib documentation.

    Michael

Re: Error Handeling in Sybase::Dblib
by PodMaster (Abbot) on Jun 25, 2004 at 09:37 UTC
    The module is called Sybase::DBlib (CASE MATTERS!!!).
    I know nothing about Sybase::DBlib , but if dbsqlexec is printing errors to STDOUT (if on the screen means STDOUT ), you should try something like (untested):
    my $err = ""; { require IO::String; local *main::STDOUT = $err = IO::String->new; $tcm->dbsqlexec || die "Error in dbsqlexec"; }
    now $err contains whatever dbsqlexec printed to STDOUT.

    update: A better idea is to use select, as in (also untested):

    my $err = ""; { my $orig = select IO::String->new(\$err); $tcm->dbsqlexec || die "Error in dbsqlexec"; select $orig; }

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.