[prev in list] [next in list] [prev in thread] [next in thread] 

List:       apache-modperl
Subject:    Re: RFC: Apache::Carp - Error Handling under mod_perl
From:       "Thomas J. Mather" <tjmather () anidea ! com>
Date:       2000-11-30 16:14:51
[Download RAW message or body]

I forgot to include the Apache/Carp.pm attachment in my last e-mail.
Here it is.

["Carp.pm" (TEXT/PLAIN)]

package Apache::Carp;

# $Id: $

use integer;
use strict;

use Mail::Mailer;

# trap die and warn

$main::SIG{__WARN__} = \&Apache::Carp::warn;
$main::SIG{__DIE__} = \&Apache::Carp::die;

$Apache::Carp::in_use = 'yes';

sub errorMessage {

  return if $Apache::Carp::in_use eq 'no';

  my $r = Apache->request;

  my $s = Apache->server;

  return unless $r;

  if($r->dir_config('APACHE_CARP_HANDLER') eq 'email'){

    my $uri = (split(' ',$r->the_request))[1];
    $uri .= '?' . $r->notes('query_string') if $uri !~ /\?/;

    my $userID = $r->connection->user;

    my $host = $r->header_in('Host');
    my $remote_host = $r->header_in('X-Forwarded-For') || $r->get_remote_host;
    my $referer = $r->header_in('Referer');

    my $current_callback = $r->current_callback;

    my $message = <<END;
$uri
userID: $userID  host: $host  remote_host: $remote_host  referer: $referer
handler: $current_callback

$_[0]

END
    my $i = 0;
    while (my ($package, $filename, $line, $subr) = caller($i)){
      $message .= "stack $i: $package $subr line $line\n";
      $i++;
    }
    my $mailer = new Mail::Mailer;
    $mailer->open({To => $s->server_admin,
		   Subject => "Website $_[1]"
		  });
    print $mailer $message;
    $mailer->close;
  } elsif ($r->dir_config('APACHE_CARP_HANDLER') eq 'display') {
    my $color = $_[1] eq 'WARN' ? 'blue' : 'red';
    my $message = $_[0];
    $message =~ s/</&lt;/g;
    $message =~ s/>/&gt;/g;
    print qq{<pre><font color="$color">$_[1]: $message};
    my $i = 0;
    while (my ($package, $filename, $line, $subr) = caller($i)){
      print "stack $i: $package $subr line $line\n";
      $i++;
    }
    print qq{</font></pre><br>};
  }
}

sub warn {
  &errorMessage($_[0],"WARN");
}

sub die {
  &errorMessage($_[0],"FATAL");
}

1;

__END__

=head1 NAME

Apache::Carp - Error Handling under mod_perl

=head1 SYNOPSIS

In your perl code or C<startup.pl> file:

  use Apache::Carp;

In your Apache configuration file:

  PerlSetVar APACHE_CARP_HANDLER email

=head1 DESCRIPTION

Redirects warnings and fatal errors to screen or e-mail by using
C<__WARN__> and C<__DIE__> signal handlers.  Includes detailed information
including error message, call stack, uri, host, remote host, remote user,
referrer, and handler.

If C<APACHE_CARP_HANDLER> is set to I<display>, errors will be
displayed on the screen for easy debugging.  This should be used in a development
environment only.

If C<APACHE_CARP_HANDLER> is set to I<email>, errors will be e-mailed to the site
adminstrator as specified in the Apache C<ServerAdmin> configuration directive.
This should be used on a production site.

=head1 AUTHOR

T.J. Mather (tjmather@anidea.com)

=head1 COPYRIGHT

Copyright (c) 2000, AnIdea Corporation.  All rights Reserved.

=head1 LICENSE

This program is distributed in the hope that it will be useful, but WITHOUT ANY \
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A \
PARTICULAR PURPOSE. See the Ricoh Source Code Public License for more details.

You can redistribute this module and/or modify it only under the terms of the Ricoh \
Source Code Public License.

You should have received a copy of the Ricoh Source Code Public License along with \
this program; if not, obtain one at http://www.pagekit.org/license

=cut



---------------------------------------------------------------------
To unsubscribe, e-mail: modperl-unsubscribe@apache.org
For additional commands, e-mail: modperl-help@apache.org

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic