Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

'die' command won't print error messages 1

Status
Not open for further replies.

Kathy1

Programmer
Dec 21, 2000
39
US
My 'die' commands have never worked...if a file access really doesn't work, I just get a blank page and then have to go back and put a bunch of: print "you are here1"; type of displays in my code to see where I got to.

Here is an example one that doesn't work. (This is for DB2)
$DBH=DBI->connect('DBI:ODBC:test', 'db2', 'testpass') or
die "Could not connect to the agency database";

I can change my database name from 'test' to one that doesn't exist, and the program definitely jumps off a cliff, but I don't get the message. I get a blank screen. This has been a major headache for me for quite a while. All of my 'die' commands in the system are acting this way.

Any ideas on this would be greatly appreciated.

Thanks!

Kathy
 
die usually prints to the error log. If you want it to print to the screen, you either have to set your server configuration to print the error log entry to your server error screens, or you can create a cgidie function (such as provided by cgi-lib.pl or cgi_lite module) which first outputs the error formatted in html and then dies.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Well.. if you want to see the errors, you need to redirect STDERR from the logfile to STDOUT.. you do this with:
open (STDERR, ">&STDOUT");

You can also set a handler for the death of the script. This is done with $SIG{__DIE__}. You set this to a reference to the subroutine you want to run. For instance,

sub deadly_error($) {
print "Content-type: text/html\r\n";
print &quot;<h1>Error!</h1>$_[0]&quot;;
exit;
}
$SIG{__DIE__} = \&amp;deadly_error;
die(&quot;Darnit!&quot;);

Will send an HTML error then gracefully exit. I don't know if there's any real point to the latter way, but it's a neat trick.

brendanc@icehouse.net
 
Thank you Sophisticate and Tanderso. I will try this - I don't have the code to redirect the error messages to the screen in my Perl script, so that is probably just what I need.

Appreciate the help!

Kathy :)
 
I've tried the command

open (STDERR, &quot;>&amp;STDOUT&quot;);

in a couple of my .pl programs, and it has made no difference. Can you tell me if there is a 'use' file that I need to have with it? I'm currently using several - ones that I have written, and then the following:


require &quot;cgi-lib.pm&quot;;
use CGI qw:)standard);
use DBI;

Thanks!

Kathy

 
If you don't want to bother with handlers and streams, just do this:

command || &amp;CgiDie(&quot;There was an error with command: $!&quot;);

sub CgiDie
{
my ($error) = @_;

print $error;
die $error;
}

where &quot;command&quot; is whatever command you're executing, such as &quot;open()&quot;, etc.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
I thought this was my question. Any hoop I asked a similar question about a month ago and there were different responses. Check out thread number 219-37486 and see if you can get anything from there. if not look on about 11/20 for a die question from jerehart

Miah
 
This may seem a little late, but doesn't the Perl special variable $! catch the error?

Everytime I have used die, I always do something like:

open(FILE, &quot;file.txt&quot;) || die(&quot;cant open file: $!&quot;);

Does this work as well?

-Vic

vic cherubini
malice365@hotmail.com
====

Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director

Wants to Know: Java, Cold Fusion, Tcl/TK

====
 
Ahh, I see.

Heh, sorry, didn't understand the original question.

-Vic

vic cherubini
malice365@hotmail.com
====

Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director

Wants to Know: Java, Cold Fusion, Tcl/TK

====
 
When I want a little more control over how a CGI script bails out, I use a sub showError, like......

open(HANDLE,&quot;>someFile&quot;) or &amp;showError($!);

sub showError
{
my @error = @_;
print &quot;ERROR - @error<BR>\n&quot;;
print '</BODY></HTML>';
exit;
}


This assumes the open HTML....to....BODY tags have already been sent to the browser. If not, add them to the top of the sub. .....Easily tweaked to give more or less info as needed.

'hope this helps




keep the rudder amid ship and beware the odd typo
 
Happy new year! :)

Thank you all so much for your help! I've ended up using code similar to what goBoating had suggested. Found some code already made in one of my Perl libraries, and utilized it. Interestingly enough, I get my hard-coded error messages now, but I'm still trying to get the system error message.

Here is how I coded my test connection to a DB2 database, with the database name spelled incorrectly so that it will error.

$DBH=DBI->connect('DBI:ODBC:testit', '', '') or &amp;CgiDie(&quot;Could not connect to the agency database. $!&quot;);

I receive the 'could not connect to the agency database.' part of the message, but not the $! part. I will continue working on this to get the other piece of the message. Those system messages can be invaluable in solving problems!

Thanks!

Kathy
 
.....thinking out loud (or into a keyboard, as the case may be).....Is it possible that the DBI interface is doing something with its error message other than send them to STDERR? Maybe there really is nothing in $!. There may be a method for retrieving the most recent error from a DBI operation.....??




keep the rudder amid ship and beware the odd typo
 
That is entirely possible. I am looking into it in my DB2 documentation. I'll be sure to let you all know if that is the case.

Thanks goBoating! :)

Kathy
 
Thanks tanderso.

The info I had for DB2 was to use the following code:

$DBH->errstr

but it isn't doing anything for me yet. I'll try the DBI version that you've listed and see if I can get that to do something for me.

Kathy
 
$drh=DBI->install_driver($db_type);
$dbh=$drh->connect($db_username,$db_database,$db_password) || &amp;CgiDie(&quot;Error connecting: $DBI::errstr\n&quot;);


Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Ok - I've got it now! Thanks so much for all of your help. Here is what I did:

$DBH=DBI->connect('DBI:ODBC:test1', ' ', ' ') or &amp;CgiDie(&quot;Could not connect to the agency database in sub ReadAgentFile in AgentFile.pm.&quot; . $DBI::errstr);

Coding my statements this way gave me my hard-coded error message, as well as the message from the database system. I did not need to do the driver install as my Tech. Services dude already had done that.

The &amp;CgiDie routine is one that I retrieved from our library of routines. If anyone needs it, I will be happy to post it for you.

Thank you all so very much!!

And happy new year!

Kathy
 
I'm glad I could help.

The &quot;install&quot; step is not the software installation of the DBD module for your database, it is the temporary utilization of that driver for this instance. You are simiply calling it a different way by using DBI:ODBC:test1.

Everyone has access to the &amp;CgiDie routine from the cgi-lib.pl library or the CGI_Lite module, each of which you can easily find on or Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top