Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Catching Apache error with a Perl script

by TheProphet (Initiate)
on Aug 20, 2003 at 07:06 UTC ( #285156=perlquestion: print w/replies, xml ) Need Help??

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

Hi y'all,

I'm handling Apache errors through seperate shtml files. That surely looks better than the standard error pages by the browser etc. I did this by coding in httpd.conf lines like:

  ErrorDocument 403 http://levi.prodeo/errors/err_403.shtml
  ErrorDocument 404 http://levi.prodeo/errors/err_404.shtml
  ErrorDocument 405 http://levi.prodeo/errors/err_405.shtml

etcetera.

Now, I would like to handle those through a Perl script file. For that purpose I coded in httpd.conf lines like:

ErrorDocument 404 http://levi.prodeo/cgi-bin/http_err.pl?err=403
ErrorDocument 404 http://levi.prodeo/cgi-bin/http_err.pl?err=404
ErrorDocument 405 http://levi.prodeo/cgi-bin/http_err.pl?err=405

It does not complain about this URL, but the big question is:

How do I get that error number into my Perl program?

Please spare me the CPAN modules, I think they're overweight. I'd like to see what I'm coding, so simple solutions if possible...

Tnx in advance,

-------------------------
TheProphet is seeking...

-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GAT/CS/CM/O d+(--) s++:+>: a? C++(+++)$ UL++(+++) P++>+++ L++>+++ E- W+++$ N++ !o !K w !O M- V? PS->$ PE+(-) Y+ PGP t+ 5? !X R- tv b+(+++) DI? !D G(-) !e h---- r+++ y?
------END GEEK CODE BLOCK------
http://www.geekcode.com

  • Comment on Catching Apache error with a Perl script

Replies are listed 'Best First'.
Re: Catching Apache error with a Perl script
by valdez (Monsignor) on Aug 20, 2003 at 07:50 UTC

    Apache ErrorDocument directive issues an internal redirection filling your %ENV with special fields identified by REDIRECT_ prefix containing useful informations. Besides that, you can use the usual CGI methods inside your script to retrieve passed parameters. Here is a little snippet that will show the values available to your "error handler":

    #!/usr/bin/perl use CGI; use Data::Dumper; my $q = CGI->new; %v = $q->Vars; print $q->header; print "<pre>\n"; print Dumper(\%v), "\n"; print Dumper(\%ENV), "\n"; print "</pre>";

    Please note that you don't need to pass the original error code (ie 404, etc), because that code is already available via REDIRECT_STATUS.

    Ciao, Valerio

Re: Catching Apache error with a Perl script
by tcf22 (Priest) on Aug 20, 2003 at 10:00 UTC
    The way I normally do it, is use HTML::Template, so you have a Error page template, then just load error message into it.

    Template:
    <hmtl> <head> <title> Error <!-- TMPL_VAR NAME="err" --></title> </head> <body> <H1>Error <!-- TMPL_VAR NAME="err" --> Occured</H1><BR> Explanation: <!-- TMPL_VAR NAME="Explain" --> </body> </html>
    Perl Code:
    use strict; use CGI; use HTML::Template; my $cgi = new CGI; my $template = HTML::Template->new( filename => 'error.tmpl', associate => $cgi, die_on_bad_params => 0 ); my $explanation = &get_explanation($cgi->param('err')); $template->param(Explain => $explanation); print $cgi->header(), $template->output(); sub get_explanation{ my $err = $_[0]; ##Get Explanation }
Re: Catching Apache error with a Perl script
by CombatSquirrel (Hermit) on Aug 20, 2003 at 07:44 UTC
    I think that you will hardly get any solution without a module. Besides, as far as I know, CGI is already in the standard distribution, so that it won't matter for you anyways. And your code will be much more readable if you have the networking stuff in another module.
    I've never done any CGI programming or such, since I don't have a webserver which I could use, but the following should (possibly after debugging) work:
    #!perl use strict; use warnings; use CGI qw[:standard]; param() or die "Need to provide a parameter\n"; param('err') or die "Need to provide error number\n"; print header, start_html('My error report'), "Hey, I got error ", em(param('err')), p, ". Great, isn't it?", hr;
    Hope this helps anyways.
Re: Catching Apache error with a Perl script
by Dog and Pony (Priest) on Aug 20, 2003 at 11:58 UTC
    You can have a look in $ENV{QUERY_STRING} - but really, save yourself and anybody else involved a lot of time and trouble in the future and get over the notion that CPAN modules are "overweight" or bad in any way. You are just hurting yourself, though I understand that you think you are "optimizing".

    Quick update: That might be $ENV{REDIRECT_QUERY_STRING} since it is in effect, a redirect. Needless to say, CGI.pm handles both behind the scenes so you don't need to worry about it. See?


    You have moved into a dark place.
    It is pitch black. You are likely to be eaten by a grue.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://285156]
Approved by broquaint
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2020-04-08 06:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The most amusing oxymoron is:
















    Results (43 votes). Check out past polls.

    Notices?