You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by David Hofmann <el...@gmail.com> on 2011/10/25 17:19:59 UTC

Mod Perl - Custom Error Pages

For a while now my system has error trapped issues the it encounters
and printed an error page with a 200 status code.

Recently when my web developer started using AJAX, this became a
problem, be cause he couldn't check for error codes on the pages. So I
updated my error routine to pass proper error code like:

print "Content-type: text/html\n";
print "Status: 400 Bad Request", "\n\n";

The problem I am however encountering is my custom error page
generated by the perl script is displayed and then bellow it on the
same page is the error page defined in the apache conf for that code.

The reason I want the mod perl to generate the error page is I want to
be able to pass error info in the html comments.

Is there a way to tell get mod perl to tell apache to not send the
default page also?

David

Re: Mod Perl - Custom Error Pages

Posted by Randolf Richardson <ra...@modperl.pl>.
> Hi Randolf,
> 
> Thanks for the information, the source code gives me some idea of
> where I'm going to go look next.
> 
> However directive I won't work for what I'm trying to do as I'm trying
> to handle the error code inside my the script rather and over ride the
> Apache directives. That way if it's an error my script error system
> can handle like a DB error it will produce the custom error page, but
> if it's something server related like 404 the normal errrordocument
> directives will handle it.

	You can use this subroutine (it works well for me, but I'm always 
open to suggestions for improvement), although you may need to alter 
the first two lines of code to get $r working correctly for your 
installation -- if you simply pass $r and your module isn't 
represented by a "blessed" object, then you can just replace those 
two lines with:  my $r = shift;

# =============================================================================
# Generates a genuine "404 Not found" error status.
#
# Note:  Must be called BEFORE sending any HTML.  If called afterwards, than a
#        "Document has moved HERE" link will be sent "magically."
#
# Parameters:  None.
#
# Returns:  Nothing because program execution ends here.
# =============================================================================
sub goto_404 {

  # -----------------------------------------------------------------------------
  # Initialize internal variables.
  # -----------------------------------------------------------------------------
  my $self = shift; # --- Get hashref from blessed object
  my $r = $self->{r}; # --- Apache2::RequestRec

  # -----------------------------------------------------------------------------
  # Set HTTP headers.
  # -----------------------------------------------------------------------------
  $r->status(Apache2::Const::HTTP_NOT_FOUND);

  # -----------------------------------------------------------------------------
  # Terminate the process here with the appropriate exit code so that the Apache
  # HTTPd server will definitely know what to do.
  # -----------------------------------------------------------------------------
  exit(Apache2::Const::HTTP_NOT_FOUND);

  # -----------------------------------------------------------------------------
  # This won't happen, but it helps compilers and debuggers.
  # -----------------------------------------------------------------------------
  return Apache2::Const::HTTP_NOT_FOUND;

} # -x- sub goto_404 -x-

[End of reply.]

> David
> 
> On Tue, Oct 25, 2011 at 5:26 PM, Randolf Richardson <ra...@modperl.pl> wrote:
> >> For a while now my system has error trapped issues the it encounters
> >> and printed an error page with a 200 status code.
> >>
> >> Recently when my web developer started using AJAX, this became a
> >> problem, be cause he couldn't check for error codes on the pages. So I
> >> updated my error routine to pass proper error code like:
> >>
> >> print "Content-type: text/html\n";
> >> print "Status: 400 Bad Request", "\n\n";
> >>
> >> The problem I am however encountering is my custom error page
> >> generated by the perl script is displayed and then bellow it on the
> >> same page is the error page defined in the apache conf for that code.
> >>
> >> The reason I want the mod perl to generate the error page is I want to
> >> be able to pass error info in the html comments.
> >>
> >> Is there a way to tell get mod perl to tell apache to not send the
> >> default page also?
> >
> >        Yes, you can use the ErrorDocument directive to specify a perl
> > script that's a part of your web site (instead of a .html page).
> >
> >        I wrote a document that includes an example of the 404 handler perl
> > script, here:
> >
> >                § 404 Not found handler
> >                http://www.modperl.pl/how-to/scripts.pl#404handler
> >
> >        The entire guide, which also explains how to configure the <Files>
> > stanza to handle .pl files, begins here:
> >
> >                How to install and configure ModPerl 2
> >                http://www.modperl.pl/how-to/
> >
> >         (I use this approach with my web sites, and I'm aware of a few
> > people who've followed the instructions in my guide to get ModPerl 2
> > up-and-running for their projects.)
> >
> >        Please note:  I'm not suggesting that you set up your site in the
> > same manner as mine {as there is typically more than one way to do
> > things in Perl}, but I'm hopeful that this can at least provide you
> > with some ideas that will help you create the best solution.
> >
> > Randolf Richardson - randolf@inter-corporate.com
> > Inter-Corporate Computer & Network Services, Inc.
> > Beautiful British Columbia, Canada
> > http://www.inter-corporate.com
> >
> >
> >


Randolf Richardson - randolf@inter-corporate.com
Inter-Corporate Computer & Network Services, Inc.
Beautiful British Columbia, Canada
http://www.inter-corporate.com



Re: Mod Perl - Custom Error Pages

Posted by Randolf Richardson <ra...@modperl.pl>.
> For a while now my system has error trapped issues the it encounters
> and printed an error page with a 200 status code.
> 
> Recently when my web developer started using AJAX, this became a
> problem, be cause he couldn't check for error codes on the pages. So I
> updated my error routine to pass proper error code like:
> 
> print "Content-type: text/html\n";
> print "Status: 400 Bad Request", "\n\n";
> 
> The problem I am however encountering is my custom error page
> generated by the perl script is displayed and then bellow it on the
> same page is the error page defined in the apache conf for that code.
> 
> The reason I want the mod perl to generate the error page is I want to
> be able to pass error info in the html comments.
> 
> Is there a way to tell get mod perl to tell apache to not send the
> default page also?

	Yes, you can use the ErrorDocument directive to specify a perl 
script that's a part of your web site (instead of a .html page).

	I wrote a document that includes an example of the 404 handler perl 
script, here:

		§ 404 Not found handler
		http://www.modperl.pl/how-to/scripts.pl#404handler

	The entire guide, which also explains how to configure the <Files> 
stanza to handle .pl files, begins here:

		How to install and configure ModPerl 2
		http://www.modperl.pl/how-to/

	 (I use this approach with my web sites, and I'm aware of a few 
people who've followed the instructions in my guide to get ModPerl 2 
up-and-running for their projects.)

	Please note:  I'm not suggesting that you set up your site in the 
same manner as mine {as there is typically more than one way to do 
things in Perl}, but I'm hopeful that this can at least provide you 
with some ideas that will help you create the best solution.

Randolf Richardson - randolf@inter-corporate.com
Inter-Corporate Computer & Network Services, Inc.
Beautiful British Columbia, Canada
http://www.inter-corporate.com



RE: Mod Perl - Custom Error Pages

Posted by Andreas Mock <an...@web.de>.
Hi David,

there is
http://www.gossamer-threads.com/lists/modperl/modperl/100379?search_string=error%20page%20mock;#100379

thanks to Torsten.

And here is a snippet, I use

...
    elsif($response->status == Apache2::Const::HTTP_NOT_FOUND) {
        $r->status($response->status);
        # Cookies trotzdem mitschicken
        foreach my $cookie ($response->headers->header('Set-Cookie')) {
            $r->err_headers_out->add('Set-Cookie' => $cookie);
        }
        if($response->body()) {
            $r->subprocess_env('suppress-error-charset' => 1);
            $r->content_type($response->headers->header('Content-Type'));
            $r->err_headers_out->add('Content-Type' => $response->content_type());
            $r->custom_response(Apache2::Const::HTTP_NOT_FOUND, $response->body());
        }
        return Apache2::Const::OK;
    }
...

It's just for example, but you should get the point. $response is an object I return
from the functions doing the stuff. It encapsulates several aspects. I'm pretty
sure you can read that. $r is the well known mod_perl-object.

There is an issue with older IE-Browser which present the own message if the
message given to an error status is too short. You have to google for that.
As far as I can remember the solution is to extend the string. But I never bothered
about crappy browsers in error handling.

The line >>> if($response->body()) { <<< is interesting as it triggers the printing of
your own message or - when not given - the error mechanism described by
Torsten. So you can have default error pages, e.g. nicely formatted pages for
customers saying something meaningful like 'A error has occurred'. Or you can
feed informations for certain cornercases or debugging.

Hope I could help. Greetings to Torsten.

Best regards
Andreas Mock


> -----Original Message-----
> From: David Hofmann [mailto:elmic11111@gmail.com]
> Sent: Tuesday, October 25, 2011 5:20 PM
> To: modperl@perl.apache.org
> Subject: Mod Perl - Custom Error Pages
> 
> For a while now my system has error trapped issues the it encounters
> and printed an error page with a 200 status code.
> 
> Recently when my web developer started using AJAX, this became a
> problem, be cause he couldn't check for error codes on the pages. So I
> updated my error routine to pass proper error code like:
> 
> print "Content-type: text/html\n";
> print "Status: 400 Bad Request", "\n\n";
> 
> The problem I am however encountering is my custom error page
> generated by the perl script is displayed and then bellow it on the
> same page is the error page defined in the apache conf for that code.
> 
> The reason I want the mod perl to generate the error page is I want to
> be able to pass error info in the html comments.
> 
> Is there a way to tell get mod perl to tell apache to not send the
> default page also?
> 
> David