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

I read over I want errors!! and looked into use CGI::Carp('fatalsToBrowser'), which does, exactly as it implies. My script however will be used by end users that will more than likely have a huge freak out factor going when they see either informative or non-informative errors. Is it possible to have || die that will provide a url redirect? I would ask if it were possible to also provide a redirect for fatals, but that seems like an oxymoron.

humbly -c

Replies are listed 'Best First'.
Re: DIE or Fatals cause Redirect?
by arturo (Vicar) on Jul 19, 2001 at 19:59 UTC

    For errors that occur after you've already spit out the header, you can write a custom handling routine (e.g. use or bail_gracefully($!) instead of die, and have bail_gracefully do whatever needs doin' with $!.)

    Most HTTP servers have a facility for generating custom error pages -- you can even set up a CGI to handle them. So read up on your webserver's documentation. Under Apache, it can be as simple as adding the line

    ErrorDocument 500 /cgi-bin/error_handler.pl

    Which says "if there is an error with code 500 (caused by a program's failing to execute, or to output proper headers), redirect the user to this URI."

    HTH

    perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
      One note on the ErrorDocument 500 method: make sure if you use a .pl script for this rather than a static .html file that the script is guaranteed to execute flawlessly, or you get into a potentially nasty situation.
Re: DIE or Fatals cause Redirect?
by lshatzer (Friar) on Jul 19, 2001 at 19:55 UTC
      Beware signals..
      It's quite possible that the signal being handled at a 'bad time' will actually cause the process to segfault, which is worse than the initial behaviour that's being circumvented.

      Malk
Re: DIE or Fatals cause Redirect?
by wardk (Deacon) on Jul 19, 2001 at 20:02 UTC

    Create an error routine to execute when something goes wrong, present a happy screen to the users letting them know that some sort of non problematic (don't panic!) problem has occured and to contact X if they desire.

    then internally, log what really happened (the web logs might suffice instead), or perhaps shoot an email to yourself with the real error so you can dig in and fix it.

    for instance, if we can't connect to the database, rather than present the ORA error messages, we just post a "system down" message that assures the users that it's temporary, and provides information for them to contact support.

    good luck!

Re: DIE or Fatals cause Redirect?
by tachyon (Chancellor) on Jul 20, 2001 at 14:32 UTC

    Hi, this is the code I use in my scripts. I use DieNice instead of die. You can save recoding all your dies by setting a $SIG{__DIE__} = \&DieNice as noted above. In my experience the die sig handler is not reliable on Win32.

    sub DieNice { my $message = shift; my ($package, $file, $line) = caller(); my $user_message = "Sorry, the system is currently unable to proce +ss your request<br>\n"; $user_message .= "due to routine maintenance. Please try again lat +er. Our Apologies\n"; $message = Unindent <<" MESSAGE"; A fatal die was trapped by the $scriptname die nice routine, detai +ls: Time: $datetime List name: $list_name Script name: $scriptname Package: $package File: $file Line Number: $line Error Message: $message MESSAGE &TellUser($user_message); # the bullshit message! &WarnAdmin($message); # the real facts! exit; } sub WarnAdmin { my $message = shift; my ($old_name, $old_email) = ($name, $email); $name = "Administrator"; $email = $our_email; &EmailUser ($message); ($name, $email) = ($old_name, $old_email); return; }

    The &TellUser sub displays a Sorry page with the passed message. The old routine maintenance story!. The unindent sub just strips leading whitespace and lets the herepages sit nicely inline in the code.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: DIE or Fatals cause Redirect?
by one4k4 (Hermit) on Jul 19, 2001 at 20:49 UTC