[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

Handling fatal errors while using HTML::Template

My script makes db and ssh connections to several servers and rather than using 'die' (and CGI::Carp) when/if there is a fatal error, I want to use a sub that assigns one or more template vars then output and exit.  However, I've been unable to get this to work as desired.  If I leave in "use CGI::Carp qw(fatalsToBrowser)", then instead of executing the sub and displaying the error in the template, it produces the standard die error.  If I comment out CGI::Carp, then it returns a blank page.

Here are the relevant portions of the script and template.

my $dbh = DBI->connect("DBI:mysql:$db:$server", $user, $pass,
                         { RaiseError => 1, PrintError => 0 } )
              or fatal($DBI::errstr);

sub fatal {
   my $error = shift;
   $template->param(fatal_error => $error);
   $template->output;
   exit;
}


<!-- container to display fatal error messages -->
<TMPL_IF NAME="fatal_error">
  <div class="status_window">
    <h3>FATAL ERROR</h3>
    <p><TMPL_VAR NAME="fatal_error"></p>
  </div>
</TMPL_IF>
<!-- end of error container -->

I also tried wrapping the db call in an eval block and execute the fatal sub if an error is caught, but received a blank page.

Anyone know the proper way to handle fatal errors cleanly while using HTML::Template?
0
FishMonger
Asked:
FishMonger
  • 4
  • 3
1 Solution
 
mjcoyneCommented:
Try:

$SIG{__DIE__} = sub {
   my $error = shift;
   $template->param(fatal_error => $error);
   $template->output;
   exit;
}
0
 
FishMongerAuthor Commented:
That gave me a compilation error until I realized that it was missing the semi-colon at the end.  Once I added the semi-colon, it returned a blank page.  Actually, the page comes up normally initially, but returns a blank page when I submit the form which does the db query.

However, your suggestion made me take a closer look at it and I found the problem.

$template->output;

should be
print $template->output;
0
 
mjcoyneCommented:
So, what ultimately worked was:

$SIG{__DIE__} = sub {
   my $error = shift;
   $template->param(fatal_error => $error);
   print $template->output;
   exit;
};

?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
FishMongerAuthor Commented:
Yes, modifying the die handler like that works, but so does my original sub.  The key fix was adding the missing print keyword, which was a dumb mistake on my part.

I'm currently using the modified die handler while I'm in the development stage, however I don't see its advantage over using the fatal sub.  In fact it might have a slight disadvantage (in style only).  I prefer to place all of my subs near the end of the script, but I think the reassignment of the die handler needs to be placed near the top.
0
 
mjcoyneCommented:
I agree, there's likely no benefit because, in this case, the error you're trapping is not fatal from the Perl script's point of view -- the script will continue even if $DBI::errstr is populated.

Thanks for the points anyway...:)
0
 
FishMongerAuthor Commented:
You're welcome :)

One thing I haven't yet figured out is now that we can cross post the questions in multiple areas, which area do the points get applied to?  Did you get these points in Perl or CGI or both?
0
 
mjcoyneCommented:
I asked the same question -- see http://www.experts-exchange.com/Community_Support/Community_Advisor/ExpertCare/Complaints/Q_22434410.html (still not sure of how to use the shorter EE links...).

Apparently, the points are awarded in both areas.  
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now