Yeah, CGI::Carp will do exactly what you want.  I trap all system errors
(DBI, die, etc) using Carp and format the results into a nice webpage for
the user and further, I email myself the error along with the environment
variables in use at the time as well as a stack dump of where it occured.
This has been a life saver for me.  I put this in a module that is included
in all perl scripts.

Here's my code snippet that does all the work, it might give you some ideas
.....

use CGI::Carp qw(fatalsToBrowser set_message carpout) ;

$SIG{'__DIE__'} = \&handle_errors;      # This just makes sure we catch
errors and send to handle_errors

# BEGIN ensure this always gets executed first
BEGIN {

  sub handle_errors {

        $| = 1 ;   # Flush all output

        my $msg     = shift;
        my $docroot = "$ENV{SITE_ROOT}/html" ;

        # Default is the default error template
        my $file = "blank_app_error.html" ;

        print "Can't open error file *$ENV{SITE_ROOT}/html/$file* :: $msg"
if !-e "$ENV{SITE_ROOT}/html/$file" ;
        set_message("Error in error handling: $msg") ;

        my $text = qq{
                System Error: $msg <p>This error has already been forwarded
to our support department.<p>
                DBname: $db  DBhost: $dbhost Site: $ENV{SITE123}
        } ;

        my $jstext = $text ;
        $jstext =~ s/<p>/\\n\\n/gsi ;    # Convert paragraphs to newlines
        $jstext =~ s/(')|(")/\\$1/gsi ;  # convert embedded quotes with
escaped versions
        $jstext =~ s/\015\012/\\n/gsi ;  # convert the crlf, otherwise
javascript will fail
        $jstext =~ s/\015|\012/\\n/gsi ; # convert the cr or the lf,
otherwise javascript will fail
        $jstext =~ s/<CODE>//gsi ;       # remove the CODE tag

        # let's make this an alert which ensures its always coming up, even
inside a hidden iframe
        $script = "<script>alert('$jstext'); </script>" ;
        $text = $script . $text ;

        print "Content-type: text/html\n\n" ;

        # Readtemplate is my local routine to read a file and do some custom
parsing on it
        my $form = readtemplate("$ENV{SITE_ROOT}/html/$file") ;
        $form =~ s/<ERROR_MSG>/$text/ ;

        # display message to user
        print "<pre>$form" ;

    ## Now we pick up additional information to mail to ourselves to derive
the context

        my $stack = CGI::Carp::_longmess() ;    # Get the full stack of what
happened
        $stack =~ s/eval '.+?PerlRun\.pm line \d+\n//gsi ;  # Remove any
evals since we are in PerlRun environment
        $text .= "\n\n<pre>Call Stack is:\n\n$stack\n\n" if $stack ;

    # Send/queue an email for this error to the webmaster

    # Get context of calling routine
    $msg .= "\n\n$stack\n\n" if $stack ;

    if (defined %ENV) {
                use Data::Dumper ;
        $msg .= "\nEnvironment:\n" . Dumper(%ENV) ;
    }

    # My own local interface to sendmail, but essentially you mail yourself
here
    email_queue('webmaster','[EMAIL PROTECTED]',$msg,"Website Error on
$ENV{SITENAME}",$msg) ;

    exit 1 ;
  }

  set_message(\&handle_errors) ;

}


----- Original Message ----- 
From: "Martin Moss" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, June 17, 2003 7:23 PM
Subject: Error Handling/Reporting


> All,
>
> I've been hunting the web for mechanisms whereby I can trap errors from my
> mod perl objects and report them back to a user nicely, log them AND
> possibly send reports back to me.
>
> Before I started hunting I thought it would be a simple process to use the
> eval mechanism to run my scripts and then report errors back to my
browser.
> (replacing the nasty error500  document)
> In my travels I found CGI::Carp and Apache::ErrorReport
>
> Before I try to pull them both apart and rebuild something which fits my
> requirements, has anybody else done anything along these lines?
> Does anybody have any other Recomendations for me to look at?
>
> Kind regards
>
> Marty
>

Reply via email to