> -----Original Message-----
> From: Jay Strauss [mailto:[EMAIL PROTECTED]]
>
> I've tried the suggestions so far:
>
> cgi::carp
> http://perl.apache.org/guide/snippets.html#Redirecting_Errors_to_t
> he_Client
> BEGIN { print "Content-Type: text/plain\n\n"; *STDERR = *STDOUT }

Jay,

Below is a module I wrote for doing what you want.  It'll need some name
changing, but the POD at the bottom should explain mostly how to use it.  
One feature is that you can give it a list of "developer" IP addresses
that should see the Perl errors, while other people get a nice bug report
form.  It even works with mod_perl, but beware:  once you use it on one
Registry CGI, all your mod_perl CGIs handle their errors this way.

I don't think I've done everything the most proper or best way but it has
worked in a production environment for a year or so.  I'd welcome comments
or encouragement to put it in CPAN.

Cheers,
nathan
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Nathan Vonnahme             [EMAIL PROTECTED]
http://enteuxis.org/nathan  http://thethirdsector.com


# $Id: CGI_errors.pm,v 1.3 1998/08/26 01:53:55 nathanv Exp $

#  Enteuxis::CGI_errors

# improve CGI error-reporting!  Give an intelligible message to someone on a 
development machine, 
#      or a bug report form to a user and an email report to $alert_email.
#
#  see the pod documentation at the bottom for more...
#
# Copyright 1998 Internet Alaska, Inc.
# 
# written by [EMAIL PROTECTED]


package Enteuxis::CGI_errors;

use vars qw(@ISA @EXPORT $VERSION);
require Exporter;
@ISA = Exporter;
@EXPORT = qw(setup_CGI_errors);

$VERSION = sprintf("%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/);


use strict;
use CGI::Carp;

my $mailprog = '/usr/lib/sendmail';
my $FORMMAIL_URL = 'http://www.alaska.net/cgi-bin/FormMail.pl';
my $default_email = 'nathan\@enteuxis.org';

my @dev_machines = (    
        '208.151.124.132',
        'inferno.infoinsights.com',
);

my @warnings = ();

sub setup_CGI_errors {
        $| = 1;
        my $alert_email = shift || $default_email;

        $main::SIG{'__WARN__'} = sub {
                push @Enteuxis::CGI_errors::warnings, @_ ;
        };

        $main::SIG{'__DIE__'} = sub {
                my $mesg;
                # set $header to a valid HTTP header
                my $header = "Content-type: text/html\n\n";

                if ( ! $ENV{REMOTE_HOST} ||
                 (join " ", @dev_machines) =~ /\b$ENV{REMOTE_HOST}\b/ ) {
                        $mesg = "<h1>SCRIPT ERROR</h1>\n";
                        
                        $mesg .= "<h2>WARNINGS:</h2>\n<ul>\n";
                        foreach ( @Enteuxis::CGI_errors::warnings ) { $mesg .= 
"<li>$_\n"; }
                        $mesg .= "</ul>\n<br><hr>";
                        $mesg .= "<h2>DIE MESSAGE:</h2><ul><li> @_</ul><br>";

                        my($package,$file,$line) = caller();
                        $mesg .= "<b>Died</b> in $package at $file line $line.\n";
                    
                        print STDOUT $header . $mesg;
                        exit;
                }
                else {
                        my $url = 
"$ENV{SERVER_URL}$ENV{SCRIPT_NAME}$ENV{PATH_INFO}?$ENV{QUERY_STRING}";
                        $mesg = qq[
<html>
<head><title>Script Error</title></head>
<body bgcolor=#ffffff>

<h1>SCRIPT ERROR</h1>

Congratulations!  You've found a bug!  Our staff have been notified and the problem 
should be fixed soon.\n
We'd like it if you can also send us a short description of what you were trying to 
do:<hr>
<form action="$FORMMAIL_URL" method="POST">
<input type=hidden name="recipient" value="$alert_email">
<input type=hidden name="subject" value="broken CGI comment">
<input type=hidden name="the URL in question" value="$url">
Your name (optional)<input type=text name="realname" value=""><br>
Your email address (optional)<input type=text name="email" value="\@alaska.net"><br>
What were you trying to do? <textarea name="comment" rows=3 cols=40 
wrap="virtual"></textarea>
<input type=submit value="mail comments">
</form><hr>
</body></html>
                        \n\n];
                        open ERRMAIL, "|$mailprog -t" or die "couldn't open mail pipe- 
$!";
                        print ERRMAIL "To: $alert_email\n", 
                                                  "From: the.web.server\n", 
                                                  "Subject: $ENV{SCRIPT_NAME} is 
broken!\n\n";

                        print ERRMAIL "The CGI at $url died!\n\n";
                        print ERRMAIL "WARNINGS:\n", join( "\n",
                                @Enteuxis::CGI_errors::warnings ), "\n\n";
                        print ERRMAIL "DIE MESSAGE:\n@_\n\n";
                        print ERRMAIL "ENVIRONMENT VARIABLES:\n";
                        print ERRMAIL "Here are the contents of ENV when this attempt 
to run the script was made:\n";
                        foreach (keys %ENV) {  
                                print ERRMAIL sprintf("%-32s", $_), " = $ENV{$_}\n";  }
                        close ERRMAIL;

                        print "$header\n$mesg\n";
                        exit;
                }
        };
}



1;
__END__

# Below is the Plain Old Documentation. 

=head1 NAME

Enteuxis::CGI_errors - Perl extension to improve CGI error-reporting!

Give an intelligible message to someone on a development machine, or a bug
report form to a user and an email report to the script maintainer.

=head1 SYNOPSIS

        BEGIN {
            use Enteuxis::CGI_errors;
            setup_CGI_errors ('nathan\@enteuxis.org');
        }


=head1 DESCRIPTION

This module improves error handling in CGI scripts.  

If the CGI script dies after the BEGIN block is loaded, all warnings and die
messages will be printed out if the viewer's remote host is in the list of
development machines (as of June 1998, this is all sysadmin and webservices
machines at IAI).  Otherwise a nice bug report form will be displayed, and the error
messages will be mailed to the address (or addresses) that were passed to 
setup_CGI_errs().

After calling setup_CGI_errs, just use warn(), die(), or Carp functions
normally.  Warning messages will only be displayed when the script dies,
however.

=over 4

=item E-mail notification

The first argument to setup_CGI_errs should be the email address or addresses
that you want to receive bug reports and error messages from this script.  If no
email addresses are specified, errors go to [EMAIL PROTECTED]

=item Bug Report Form

If a user from a non-development machine finds a bug, the warn() and die() messages 
will be
sent by email, and a FormMail compatible bug report form will be printed out to them.  
To
change the URL used for the FormMail CGI, edit the CGI_errors.pm module itself.

=item Development Machines

Edit the variable @dev_machines in the CGI_errors.pm module itself to add or remove 
machines
from the list that will see error messages instead of the bug report form.

=back

=head1 AUTHOR

Nathan Vonnahme, [EMAIL PROTECTED]

=head1 COPYRIGHT

Copyright E<copy> 1998 Internet Alaska, Inc.  All rights reserved.

=head1 REVISION

$Id: CGI_errors.pm,v 1.3 1998/08/26 01:53:55 nathanv Exp $

=cut

Reply via email to