×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

'die' command won't print error messages

'die' command won't print error messages

'die' command won't print error messages

(OP)
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  

RE: 'die' command won't print error messages

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.
http://www.oac-design.com

RE: 'die' command won't print error messages

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 "<h1>Error!</h1>$_[0]";
  exit;
}
$SIG{__DIE__} = \&deadly_error;
die("Darnit!");

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

RE: 'die' command won't print error messages

(OP)
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 :)

RE: 'die' command won't print error messages

(OP)
I've tried the command

open (STDERR, ">&STDOUT");

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 "cgi-lib.pm";
use CGI qw(:standard);
use DBI;

Thanks!

Kathy

RE: 'die' command won't print error messages

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

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

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

  print $error;
  die $error;
}

where "command" is whatever command you're executing, such as "open()", etc.


Sincerely,
 
Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com

RE: 'die' command won't print error messages

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

RE: 'die' command won't print error messages

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, "file.txt") || die("cant open file: $!");

Does this work as well?

-Vic

vic cherubini
malice365@hotmail.com
www.epicsoftware.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
Wants to Know: Java, Cold Fusion, Tcl/TK
====

RE: 'die' command won't print error messages

vikter, the question was not how to capture errors, but how to make them print to the webpage.


Sincerely,
 
Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com

RE: 'die' command won't print error messages

Ahh, I see.

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

-Vic

vic cherubini
malice365@hotmail.com
www.epicsoftware.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash, Director
Wants to Know: Java, Cold Fusion, Tcl/TK
====

RE: 'die' command won't print error messages

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

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

sub showError
{
my @error = @_;
print "ERROR - @error<BR>\n";
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

RE: 'die' command won't print error messages

(OP)
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 &CgiDie("Could not connect to the agency database. $!");

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

RE: 'die' command won't print error messages

.....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

RE: 'die' command won't print error messages

(OP)
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

RE: 'die' command won't print error messages

DBI prints its error messages to $DBI::errstr


Sincerely,
 
Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com

RE: 'die' command won't print error messages

(OP)
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

RE: 'die' command won't print error messages

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


Sincerely,
 
Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com

RE: 'die' command won't print error messages

(OP)
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 &CgiDie("Could not connect to the agency database in sub ReadAgentFile in AgentFile.pm." . $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 &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

RE: 'die' command won't print error messages

I'm glad I could help.

The "install" 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 &CgiDie routine from the cgi-lib.pl library or the CGI_Lite module, each of which you can easily find on www.perl.com or www.perl.org.


Sincerely,
 
Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close