CGI Scripts: Catching and Displaying (Friendly) Error messages

Discussion in 'Perl Misc' started by Sam, Feb 20, 2007.

  1. Sam

    Sam Guest

    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].
     
    Sam, Feb 20, 2007
    #1
    1. Advertising

  2. "Sam" <> wrote in news:1171991711.259920.285010
    @a75g2000cwd.googlegroups.com:

    > 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.
     
    A. Sinan Unur, Feb 20, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    9
    Views:
    81,513
    Naga Syamala A
    Feb 17, 2012
  2. Kenneth McDonald
    Replies:
    1
    Views:
    391
    Peter Hansen
    Jun 16, 2004
  3. davidj411
    Replies:
    0
    Views:
    577
    davidj411
    Jun 27, 2008
  4. Praveen Bengeri
    Replies:
    1
    Views:
    322
    Tom Kaminski [MVP]
    Apr 19, 2004
  5. Stuart Palmer

    Non friendly error messages

    Stuart Palmer, Aug 11, 2004, in forum: ASP General
    Replies:
    2
    Views:
    176
    Stuart Palmer
    Aug 13, 2004
Loading...

Share This Page