In my last large CGI application, I had a sub 'print_error' defined in a module 'Shop.pm'.
I sent any fatal errors to that, e.g.
$sth = $dbh->prepare($sql) or Shop::print_error('could not connect to DB', $DBI::errstr);
The print_error sub then used HTML::Template to send a error message to the browser, and then used sendmail to send an error report to the admin.

A lot of the code was just error checking.
Due to my laziness, I'd like to cut out all of the error checking, of course.
I'm thinking I could use DBI's {RaiseError} and use Fatal to catch anything else that I consider die-able.
I've mocked up some psuedo-code below to give an idea of what I'm thinking.
My question is: Are there any likely problems with this approach and are there better ways of achieving this?
#!/usr/bin/perl -T use strict; use warnings; use CGI; use DBI; use HTML::Template; use Fatal qw/ open DBI::connect /; $SIG{__DIE__} = \&print_error; use My::Conf; my $conf = new My::Conf; ### main program goes here. sub print_error { eval { print CGI::header(); open (FILE, "< $conf->{'errorfile'}"); while (<FILE>) { print }; close FILE; } eval { # Use sendmail to send @_ and %ENV to admin. } 1; };
Note: My preference would be to print a CGI redirect rather than opening a file, which might fail.
However, I wouldn't know whether a header had already been printed or not. (If a header had already been printed, the redirect wouldn't work)

In reply to custom CGI die by fireartist

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.