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

Hi, monks.

I'm exploring Apache2:: so my question could be senseless.

I'd like to have a separate handler for displaying errors. So that any other handler which encounters an exception redirects its error message to apache2::myerror.

Now, two questions:
- is the idea itself correct, or there is a general mechanism of doing so?
- if it is, how do I redirect error message text?

Thanx,
Artem.

Replies are listed 'Best First'.
Re: mod_perl: redirecting error message
by TOD (Friar) on Apr 18, 2007 at 11:58 UTC
    put something like this in your /etc/apache2/sites-available/my-project file:
    <VirtualHost foo.bar.baz.bla> [...] ErrorDocument 500 /error/500 PerlModule MyProject::ErrorHandler [...] <Location /error> [...] SetHandler perl-script PerlResponseHandler MyProject::ErrorHandler </Location </VirtualHost>
    notice that the /error directory must not exist. it's simply a placeholder in the config file.

    how the ErrorHandler package should look like, and on how to pass the error messages to it, take a look at the mod_perl2 documentation, especially the $r->err_headers_out, $r->notes, and $r->pnotes methods. basic information on how to write a response handler you will find at http://perl.apache.org/docs/2.0/user/coding/coding.html.
      I'm trying to use pnotes to pass error message.
      Here is the module where the error case is supposed to happen:
      sub handler : method { my ($self, $r) = @_; ... $self->{r}->pnotes(error => 'Darn'); return Apache2::Const::SERVER_ERROR; } ... 1;
      Now the special error handler:
      sub handler { my $r = shift; $r->content_type('text/html'); $r->print($r->pnotes("error")); return Apache2::Const::OK; } 1;
      And finally httpd.conf part to connect it all:
      ... ErrorDocument 500 /error ... PerlModule dir_browse::error <Location /error> SetHandler perl-script PerlResponseHandler dir_browse::error </Location>
      The problem is that pnotes('error') does not keep its value when redirected to error handler (i tried to redirect explicitly using headers_out->set(Location => ...) and Apache2::Const::REDIRECT but the result was the same).
      Now, I've read in pnotes doc that "The values get reset automatically at the end of each HTTP request". Does that mean, that in my case the request is finished before getting to error handler? Or there is some other issue?

      Thanks,
      Artem.
Re: mod_perl: redirecting error message
by perrin (Chancellor) on Apr 18, 2007 at 11:57 UTC