rastoboy has asked for the wisdom of the Perl Monks concerning the following question:

Honored Monks,

I'm trying to fail informatively via CGI. The problem I have is, for example, when trying to open a Storeable session file:

my $address = retrieve("/tmp/$session_file") or die "no session file $ +!\n";
So to force failure I go delete the session file. In the logs I get:
can't open /tmp/MYyqafy9gMnlZenwi9KDmd3E4Gj4Yk5nSyVL6: No such file or + directory at ...
which is quite informative. But in my web browser I just get a blank screen.

How can I force the error to STDOUT and thence to the web browser?

Replies are listed 'Best First'.
Re: Failing noticeably in CGI
by dsheroh (Monsignor) on Nov 15, 2009 at 10:54 UTC
    As already noted, CGI::Carp will do exactly what you asked for, but...

    Consider carefully the wisdom of doing so. Many of us don't use CGI::Carp on sites which will be exposed to the public because detailed information about how and why the code failed could be very helpful to an attacker who wished to compromise the site by deliberately causing it to fail and, perhaps, even help him engineer the means to cause it to fail in a specific manner for his advantage.

    CGI::Carp is a handy development tool (especially when you don't have direct access to the server logs), but you'd probably be better off using a more verbose construct which lets you tell the user information which is both safe for you and useful for him1. Maybe even recover from the error and go on instead of dying. Something like:

    unless (my $address = retrieve("/tmp/$session_file")) { warn "no session file $!\n"; create_new_session_file("/tmp/$session_file"); redirect_user_to('new_session_page'); }


    1 OK, so the user knows there his session file should have been at /tmp/blahblahblah. What can he do about it and how does this knowledge benefit him in any (legitimate) way?

      CGI::Carp provides a handy hook for this:

      use CGI::Carp qw( fatalsToBrowser set_message );
      
      BEGIN {
          sub handle_errors {
              my $message = shift;
              # Insert some custom code here, for example check the remote address ...
              print "<h1>Oh gosh</h1>";
              print "<p>Got an error: $message</p>";
          }
          set_message( \&handle_errors );
      }
      
      dsheroh: Thanks for that, was actually contemplating that concept myself once I saw fatals going to my browser. Like you said, it is darn handy during development. And your suggestion is actually what I was originally looking for--thanks!

      WizardofUz: that's quite handy as well--y'all have given me a lot of tools to work with, and I am grateful!

Re: Failing noticeably in CGI
by WizardOfUz (Friar) on Nov 15, 2009 at 08:47 UTC
    Try use CGI::Carp qw( fatalsToBrowser );
Re: Failing noticeably in CGI
by happy.barney (Friar) on Nov 15, 2009 at 08:27 UTC
    you can use eval
    eval { your code with die here ... }; if ($@) { # print to web browser print CGI::header ...; print "Error occured: $@"; die; }
Re: Failing noticeably in CGI
by Anonymous Monk on Nov 15, 2009 at 08:54 UTC
      Rock, CGI::Carp for the win, thanks y'all. Thanks for the input too, Happy Barney--that is a good concept to keep in the old tool chest :-)