Get tired of trying to figure out what went wrong in your CGI programs when you get a 500 Internal Server Error? Maybe you can't get to your error logs for one reason or another (often a problem on shared servers)? Here's an easy way to get those errors sent to your browser instead of a 500 Internal Server Error (provided you have set permissions on the CGI program correctly and it isn't an error due to that).

This comes in really handy when debugging, but shouldn't be a permanent fixture of your CGI programs in most cases. (You wouldn't want the script to dump out it's errors every time it encountered one when running an app in production mode-- it's embarrassing! It also isn't safe and could reveal information about your server or operating system that you don't want the public to know)

Go ahead and give it a try. Write a mini-program, even with syntax errors, put it on your server, chmod perms on it to 755 or mark it as "executable" on Windowse servers, and call it from the browser.

See also: CGI::Carp

# Put this code anywhere in your script. BEGIN { $SIG{'__DIE__'} = sub { print <<__WARN__ and exit 1 } } Content-Type: text/html; charset=ISO-8859-1\n Fatal Error in @{[(caller(2))[1]||__FILE__]} at ${\ scalar localtime } while responding to request from ${\ $ENV{'REMOTE_ADDR'} || 'localhost +' } ${\ join("\n",$!,$@,@_) } __WARN__

In reply to Perl CGI Tip: Send error messages to your browser by Tommy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.