3

I'm using the following code to test the outputting of fatal errors to the browser:

use CGI;
use CGI::Carp qw(fatalsToBrowser);

die "test";

I am expecting to see some error in the browser, but there isn't any, I just get a regular 500 response. I forgot that I had custom error pages on for remote requests, and I now get Script failed to send data..

Also:

> perl -w index.pl
Status: 500
Content-type: text/html

<h1>Software error:</h1>
<pre>test at index.pl line 4.</pre>
<p>
For help, please send mail to this site's webmaster, giving this error message and the time and date of the error.

</p>
[Mon Feb  8 18:29:52 2010] index.pl: test at index.pl line 4.
  • Wouldn't it be better to test with something syntactically valid? – Anon. Feb 07 '10 at 20:17
  • If I replace the print with `print "test";` it works fine, I want to test outputting errors to the browser now. –  Feb 07 '10 at 20:18
  • And you expect syntax errors that kill Perl before your modules are even loaded to be somehow outputted? Try generating a fatal runtime error and see if that works. – Anon. Feb 07 '10 at 20:21
  • You're right. However, changing the 'print' to 'die' doesn't help much, I still get a 500. –  Feb 07 '10 at 20:24
  • 1
    You haven't gone through my Perl CGI troubleshooting guide: http://stackoverflow.com/questions/2165022/how-can-i-troubleshoot-my-perl-cgi-script – brian d foy Feb 08 '10 at 03:10
  • I updated my question (I disabled custom error pages). –  Feb 08 '10 at 17:24

1 Answers1

4

Try printing a couple of new lines before anything. That signals the end of HTTP headers to the server as the 'CGI standard' says. Or you may be able to use something like this: (as copied from the Carp man page):

use CGI::Carp qw(set_die_handler);
BEGIN {
   sub handle_errors {
      my $msg = shift;
      print "content-type: text/html\n\n";
      print "<h1>Oh gosh</h1>";
      print "<p>Got an error: $msg</p>";
  }
  set_die_handler(\&handle_errors);
}

If that doesn't work here are a few more tricks:

#!perl
BEGIN { $| = 1; print "\n\n" }
open STDERR, ">&STDOUT";

..And a few more tricks in this Perl Journal article.

mtmk
  • 6,176
  • 27
  • 32