Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

mod_perl: how to display the error message within the ErrorDocument

by projekt21 (Friar)
on Sep 30, 2002 at 13:04 UTC ( [id://201706]=perlquestion: print w/replies, xml ) Need Help??

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

I want to handle ErrorDocuments with a mod_perl module Apache::MyError in a way that the actual error message (server error 500, as written to the logs) is displayed within the result HTML page.

I know that this is evil, just treat it as an academic approach. (What I want to do in real life is mail the message to the webmaster and display a backtrace id to the user referring to that error)

What I have so far is my httpd.conf,

ErrorDocument 500 /errors/500 <Location /errors> SetHandler perl-script PerlHandler Apache::MyError Options +ExecCGI PerlSendHeader On </Location>

a script that won't compile under /usr/local/apache/cgi-bin/die.pl and the following:

package Apache::Error; use strict; use Apache::Constants qw(:common); sub handler { my $r = Apache->request; my $p = $r->prev; $r->status(OK); $r->content_type("text/html"); $r->send_http_header; $r->print("<pre>\n"); $r->print($r->path_info, "\n"); $r->print($p->notes("error-notes"), "\n"); $r->print("</pre>\n"); return OK; } 1;

The log says:

syntax error at /usr/local/apache/cgi-bin/die.pl line 6, near "if {" Missing right curly or square bracket at /usr/local/apache/cgi-bin/die +.pl line 8, at end of line Execution of /usr/local/apache/cgi-bin/die.pl aborted due to compilati +on errors. [Mon Sep 30 13:42:51 2002] [error] [client xxx.xxx.xxx.xxx] Premature +end of script headers: /usr/local/apache/cgi-bin/die.pl

$p->notes("error-notes") contains "premature end of ..." but not the "syntax error ..." which is the one I want to have.

Can someone guide me to a solution (I know one from mod_ruby using the method error_message)? Am I missing something?

alex pleiner <alex@zeitform.de>
zeitform Internet Dienste

Replies are listed 'Best First'.
Re: mod_perl: how to display the error message within the ErrorDocument
by spartacus9 (Beadle) on Sep 30, 2002 at 13:27 UTC
    Instead of Apache::Error, you might consider CGI::Carp. This would have to be done at the script level rather than the server level. There is an option with CGI::Carp that will allow you to both display the server error reason on the screen in HTML and also e-mail the error to a specified e-mail address. For instance:
    Contents of CatchErrors.pm follows: ----------------------------------- use strict; use CGI::Carp qw(fatalsToBrowser set_message); use Mail::Sender; BEGIN { sub email_problem { my $sender = new Mail::Sender({ from => 'errors@yourdomain.com', smtp => 'smtp.server.com'}); $sender->MailMsg({ to => $Error::email, subject => $ENV{SCRIPT_NAME}, msg => $_[0]}); print "Website Error"; } set_message(\&email_problem); } sub import { $Error::email = $_[1] || 'errors@yourdomain.com'; } 1; -------------------------------------- Then, in your perl script, simply include the following line: use CatchErrors qw(recipient@yourdomain.com);
    Using this method, you should be able to redirect all of the script's errors to an e-mail address you specify when you "use" the package.
Re: mod_perl: how to display the error message within the ErrorDocument
by shotgunefx (Parson) on Sep 30, 2002 at 13:29 UTC
Re: mod_perl: how to display the error message within the ErrorDocument
by zigdon (Deacon) on Sep 30, 2002 at 13:15 UTC

    I think you're confusing the apache error and the perl error. While both are written by default to apache's error log, I'm guessing the "error-notes" does not capture STDERR of your perl script.

    Not sure how you'd get the error from the script passed to apache though

    -- Dan

      Right, when I run the dying CGI under Apache::Registry I get what I want.

      But as the error is logged into my apache error log, I hoped that I could somehow get the message into my module, too. Looks like it won't work, hmmm.

      alex pleiner <alex@zeitform.de>
      zeitform Internet Dienste

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://201706]
Approved by fglock
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-25 05:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found