CGI Scripts: Catching and Displaying (Friendly) Error messages

S

Sam

I've been building a web application that accesses data from various
sources (either through APIs or scraping) and stores it in a database
(SQLite). Learning regexes, CGI, DBI, LWP, XMLRPC etc. for this has
been an exhilarating (and often frustrating) experience.

The current issue bugging me now is how to catch and display user
friendly error messages when the web app encounters any unexpected
errors - like missing Perl modules, DBI errors etc. I did come across
some web pages that suggested that this could be done using eval and
$SIG{__DIE__} but they were a bit too advanced for me (some offered
conflicting views that one shouldn't use eval / $SIG{__DIE__} ).

Please me point me to some resources that you have found helpful in
this area.

P.S: I am not looking for codes (examples are ofcourse appreciated) to
cut and paste but a basic understanding of how error catching should
be done properly in Perl CGI scripts using many modules.

[Posted through Google Groups].
 
A

A. Sinan Unur

The current issue bugging me now is how to catch and display user
friendly error messages when the web app encounters any unexpected
errors - like missing Perl modules, DBI errors etc. I did come across
some web pages that suggested that this could be done using eval and
$SIG{__DIE__} but they were a bit too advanced for me (some offered
conflicting views that one shouldn't use eval / $SIG{__DIE__} ).
....

P.S: I am not looking for codes (examples are ofcourse appreciated) to
cut and paste but a basic understanding of how error catching should
be done properly in Perl CGI scripts using many modules.

CGI::Carp may not be the right tool for the job as it is mainly intended
as a debuging tool: It may reveal too much information, and I would be
hard pressed to call its output user friendly.

The standard try-catch mechanism in Perl is eval BLOCK. Basically, you
wrap calls that might cause your program to die in eval blocks, then
check $@. If it is not empty, then an exception was thrown. You can then
show a friendly error page or try something else.

Now, with DBI, you do not need to use eval unless you have specified the
RaiseError option. If you don't specify RaiseError, you should check the
return values of DBI calls to see if the operation succeded.

To check for modules, you need to defer loading them to run time (rathen
than using the compile time use). For example:

#!/usr/bin/perl

use strict;
use warnings;

eval {
require My::Bogus::Module;
};

if ( $@ ) {
die <<MSG;
This program needs My::Bogus::Module but it was unable to load it. This
might be because the module is not installed on your system or maybe in
an nonstandard location.

Here is the error I encountered:
$@
MSG
}

__END__

C:\DOCUME~1\asu1\LOCALS~1\Temp> t
This program needs My::Bogus::Module but it was unable to load it. This
might be because the module is not installed on your system or maybe in
an nonstandard location.

Here is the error I encountered:
Can't locate My/Bogus/Module.pm in @INC (@INC contains:
C:/opt/Perl/site/lib C:/
opt/Perl/lib .) at C:\DOCUME~1\asu1\LOCALS~1\Temp\t.pl line 7.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top