At 10:01 AM 10/17/2001, Odounga y'Oyabi wrote:
>Thanks for the quick reply. I've thought of doing this. The problem 
>is I don't think it's efficient since I have to open and return a 
>template every time an exception occurred.
>
>This means, instead of two simple lines
>
>warn "blablabla!\n";
>return;
>
>I will have
>
>my $template = HTML::Template->new( filename => "error_screen" );
>  $template->param(
>                    VERSION  => $VERSION,
>                    RELEASE   => $RELEASE,
>                    RUN_MODE  => 'this_run_mode',
>                    ERROR_MSG => 'Something weird occurred!\n"
>                   );
>  return $template->output;
>
>I will have three lines of code!
>
>I was looking for something like a call to a specialized routine 
>whic�h got the error message passed in. Like the "AUTOLOAD" 
>run-mode. Unfortunately, I don't know how to pass an error message 
>to the method which is called by "AUTOLOAD" and don't even know how 
>to cause this method to be called.

Here's what I have for error handling

BEGIN
     {
     # Change the server admin address displayed in error messages
     $ENV{SERVER_ADMIN} = "gregm\@alum.wpi.edu";

     use CGI::Carp;
     # This logs all errors to my home directory since I don't have 
access
     # to the server error logs
     open(LOG, ">>/home/gregm/error.log") and CGI::Carp::carpout(LOG);
     }

use base 'CGI::Application';
use CGI::Carp qw(fatalsToBrowser);

my $template = undef;

sub Error
     {
     my $msg = shift;
     my $templ = $template;

     # If die is called before setup(), then $templ won't be defined.
     if(defined $templ)
         {
         # Prevent errors in Template from recursing
         $template = undef;

         $templ->param(Message => $msg);
         $templ->param(Error => $DBI::errstr);

         print $templ->output();
         }
     else
         {
         print "<HTML><HEAD><TITLE>Database Error</TITLE></HEAD>";
         print "<BODY><H1>Database Error</H1><P>$msg</P>";
         print "<P>$DBI::errstr</P></BODY></HTML>";
         }
     }

sub setup
     {
[...]
     $template = $self->load_tmpl('Error.html');
     CGI::Carp::set_message(\&Error);


With all this, I can now do
die("Error message");
and it will be logged to a file in my directory and reported in a 
pretty page to the user.  Note that this is using die instead of 
warn.  This means that the CGI::Application is not in control any 
more.  CGI::Carp prints out the headers, and then my Error function 
prints out the body.  When the Error function returns, CGI::Carp 
calls the real die function.

-- 
Greg Marr
[EMAIL PROTECTED]
"We thought you were dead."
"I was, but I'm better now." - Sheridan, "The Summoning"


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to