Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

mod_perl: how to display the error message within the ErrorDocument

by projekt21 (Friar)
on Sep 30, 2002 at 13:04 UTC ( #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/ 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/ line 6, near "if {" Missing right curly or square bracket at /usr/local/apache/cgi-bin/die line 8, at end of line Execution of /usr/local/apache/cgi-bin/ aborted due to compilati +on errors. [Mon Sep 30 13:42:51 2002] [error] [client] Premature +end of script headers: /usr/local/apache/cgi-bin/

$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 <>
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 follows: ----------------------------------- use strict; use CGI::Carp qw(fatalsToBrowser set_message); use Mail::Sender; BEGIN { sub email_problem { my $sender = new Mail::Sender({ from => '', smtp => ''}); $sender->MailMsg({ to => $Error::email, subject => $ENV{SCRIPT_NAME}, msg => $_[0]}); print "Website Error"; } set_message(\&email_problem); } sub import { $Error::email = $_[1] || ''; } 1; -------------------------------------- Then, in your perl script, simply include the following line: use CatchErrors qw(;
    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 <>
      zeitform Internet Dienste

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2021-04-22 18:14 GMT
Find Nodes?
    Voting Booth?

    No recent polls found