[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cgiapp] How do you handle errors?


This probably covers more than just CGI::Application, but it's very relevant
to my current application, and I'm curious to see how others have handled
it.

Typically, in my C::A apps, if I encounter an error, I do something like the
following:

sub my_runmode
{
    if($result_count > 10000)
    {
        $self->param("error", "Your search produced more than 10,000
results.  Please refine your search criteria.");
        return $self->search();
    }

    #
    # More code follows. . . 
    #
}

sub cgiapp_postrun
{
    my $self = shift;
    my $html_ref = shift;

    # Create a new instance of the page template
    my $tmpl_frame = $self->load_tmpl($config{TMPL_FRAME});

    # Dereference the output data
    my $html = $$html_ref;

    # Wrap the standard look-and-feel around the output
    $tmpl_frame->param(
        ERROR   => $self->param("error"),
        CONTENT => $html,
    );

    # Generate the new page
    $html = $tmpl_frame->output();

    # Replace old page with the new one
    $$html_ref = $html;
}

My main application template will then display an error message in big red
text should there be one.  For most circumstances, this is ok.

I've started building some modules that provide our web applications with
some of the same functions that our Windows apps have.  Getting errors (and
in some cases data) out of these functions in an application-agnostic
fashion is becoming somewhat problematic.  Some examples of what I'm
experiencing:

1) Database errors.  We log any database errors that occur, and email them
to the application's administrator.  Right now, I wrap my DBI calls in
eval{}, and then return log_error(...) if($@).  The function returns some
basic HTML markup to drop into a web page to show the user what the problem
was.  Sometimes this isn't a problem, but sometimes it is. . .  My
log_error() function knows nothing of the C::A app that caused the error,
just that an error occurred (making $self->param("error", "errormessage")
impossible).  See number 2 for some more detail.

2) Returning values from module functions.  Sometimes I return 1 for success
and 0 for failure.  Sometimes I return meaningful data, and am not sure what
to do in the case that there is an error, because I return log_error().  The
caller might confuse the HTML markup from log_error with meaningful data.
And for reasons stated above, log_error() can't set the app's error
parameter.

What have others done?  What would you do in my place?  I'm curious to hear,
because this is starting to drive me nuts ;)

Thanks in advance.  Please let me know if you want more info.

--------------------------------------------------
Jason A. Crome
Senior Software Engineer, DEVNET, Inc.
E-Mail: suppressed
http://www.devnetinc.com


---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/suppressed/
              http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: suppressed
For additional commands, e-mail: suppressed


Mail converted by mhonarc 2.6.15
This archive provided courtesy of JSW4.NET, Internet Hosting Services for Small Business.