package RSApache::Response::Exception;

use strict;
use warnings;
use Sys::Hostname;

use lib '/app/tools/common/lib';
use RSApache::Response::HTML;
use RSApache::Response;
use base 'RSApache::Response';

use Apache2::Const qw(OK REDIRECT);

use constant kTemplateNotFound => '/app/tools/common/production/html/notfound.html';

sub _init {
    my ( $self, $error ) = @_;

    $self->{error} = $error;
}

sub respond {
    my ( $self, $apache ) = @_;

    # JPK - I am beginning to suspect that not all of our perl modules
    # are handling exceptions cleanly.  Excel::Parse, I'm looking at you...
    # So, I'm adding the following call to child_terminate. This tells Apache
    # to kill this apache process after the responding to the request.
    #
    $apache->child_terminate();

    # !!!  Basically, an exception is a SERIOUS PROBLEM.
    #      So redirecting seems silly - we may end up in an never-ending
    #      cascade of exception->redirect->exception->redirects... if
    #      there is something fundamentally wrong with the handler.
    #
    #      So, what we really ought to do is just serve some HTML right
    #      here :  Don't try and do an XSL merge, don't try and do anything
    #      remotely fancy.
    #
    eval {
        my $response = RSApache::Response::HTML->new( kTemplateNotFound() );
        return $response->respond($apache);
    };
    if ($@) {
        print STDERR "EXCEPTION: $@\n";
        return 404;
    }
}

###
1;    # Play nicely.
###
