Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Catching CGI Errors

by nashdj (Friar)
on Apr 26, 2001 at 10:55 UTC ( #75726=CUFP: print w/ replies, xml ) Need Help??

I have noticed a lot of AM nodes asking about CGI errors, usually "here's my script. When I run it I get an Internal Server Error! Whats wrong with it?"

Usually the answer is "Go check your web server logs!! (grr)". Nothing wrong with that, for most cases it is where the problem will be found and solved. I'm sure there are already a hundred faqs that point to the same answer.

But for those testing scripts on some "free" web servers where you generally cant get at the logs, this is a bit difficult. Especially if the person doesnt have a computer of their own to test it on. (it might sound far fetched but in some courses it happens).

I have come up with a quick (very-simple, no doubt its been done before) solution for debugging cgi scripts. Its simply a caller script that sends errors/warnings out to stdout (the browser). This simply sets up STDERR to print to STDOUT, checks for CGI.pm checks that your script exists and then calls the script that you are debugging. Errors / Warnings will be output as they occur.

#!/usr/bin/perl -w use strict; my $scr = "script.pl"; #put name of script here print "Content-Type: text/html;\n\n"; *STDERR = *STDOUT; eval {require CGI}; print $@ if $@; if (-f $scr) { do "$scr"; print $@ if $@; } else { print "$scr not found"; }
To use:
  • Save as "debug.pl"
  • Change the $scr assignment to the script to be debugged
  • Call debug.pl from the web browser

    Note: The content type will be printed by the caller script, and as such when it is printed by the script being debugged it will appear in the browser window (this should be ignored). This is to enable any initial compiler/file errors to be printed.

  • Comment on Catching CGI Errors
    Download Code
    Re: Catching CGI Errors
    by mirod (Canon) on Apr 26, 2001 at 12:57 UTC

      What's wrong with using fatalsToBrowser?

      Put this line at the beginning of your code, et voila!

      use CGI::Carp qw(fatalsToBrowser);
        Do I feel stupid now. But hey I did throw in the 'no doubt its been done before' to cover myself :)

        I can't believe I have never seen this before, what an incrediably usefull function.

        That said, fatalsToBrowser wont pick up the warnings :)

    (Ovid) Re: Catching CGI Errors
    by Ovid (Cardinal) on Apr 26, 2001 at 21:03 UTC
      A bit off-topic, but related:

      Another thing which often causes problems is being having trouble debugging the scripts once they run properly. Debugging from the command line can be tedious, or, in the case of scripts that with high security or requiring cookies, virtually impossible without sprinkling your code with a bunch of print statements.

      To solve that problem, I once hacked together a module that allowed one to easily dump CGI variable data to the browser. It was a cheap hack designed for my personal use, but I put it on this site in case anyone could use it. It has full POD included, but it does have a couple of problems.

      • It creates a table with the data in question, but prints that data directly to the screen rather than returning it in a scalar. This caused problems with it sometimes destroying the layout of the page.
      • The other issue is that it did not handle logging the data as opposed to returning it. When I mentioned my module on Usenet, brian d foy was of the opinion that he thought it wasn't terribly useful in its present incarnation, but it might be useful if it wrote the data to another file.
      Currently, I am working on Version 3.01 of the module. It will return the table instead of printing it directly (which it should have done in the first place). Also, if you specify a filename when instantiating the object, it writes the data to an HTML document with said filename. Run the the main script, switch to a browser pointing to the debugging HTML document and hit refresh. Voilá, as many tables as you like, with all variables that you'd like to examine. The module uses Data::Dumper for formatting data.

      If any monks are interested in the new version (bugs and all), I'd be happy to provide them with a zipped copy. If I ever get the time to return to it and finish it up, I'll create a proper tar archive with tests that one can run "make" on.

      Cheers,
      Ovid

      Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

    Re: Catching CGI Errors
    by stuffy (Monk) on May 06, 2001 at 12:34 UTC
      Would it be possible for this to be turned into a module??

      Stuffy

    Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Node Status?
    node history
    Node Type: CUFP [id://75726]
    Approved by root
    help
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (7)
    As of 2014-07-13 07:55 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      When choosing user names for websites, I prefer to use:








      Results (248 votes), past polls