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

[cgiapp] Fwd: Re: [cgiapp] Re: CGI::App 3.2 proposed release available


On November 25, 2003 09:19 am, Steve Hay wrote:
> Mark Stosberg wrote:
> >>The only other thing that I'd still really like is for something to be
> >>done about the problems with exception handling that the catch/rethrow
> >>(eval/die) in run() causes.  This has been discussed at length before in
> >>several threads (see the archives), but nothing has ever happened.  Is
> >>it ever going to?
> >
> >This is not something I am fresh on or have an opinion on at the moment.
> >I know that if there is a complete patch, something is more likely to
> >happen, though. :)
>
> I'll accept that invitation!  First, a quick refresher of what the
> problem is:
>
> The code in C::A that causes problems for my exception handling is these
> two lines in the run() method:
>
>     my $body = eval { $autoload_mode ? $self->$rmeth($rm) :
> $self->$rmeth() };
>     die "Error executing run mode '$rm': $@" if $@;

[snip]

> =====
> --- Application.pm.orig    2003-11-25 14:01:29.053896600 +0000
> +++ Application.pm    2003-11-25 15:51:36.922114000 +0000
> @@ -150,8 +150,14 @@
>      }
>
>      # Process run mode!
> -    my $body = eval { $autoload_mode ? $self->$rmeth($rm) :
> $self->$rmeth() };
> -    die "Error executing run mode '$rm': $@" if $@;
> +    my $body;
> +    if ($self->can($rmeth)) {
> +        $body = $autoload_mode ? $self->$rmeth($rm) : $self->$rmeth();
> +    }
> +    else {
> +        $body = eval { $autoload_mode ? $self->$rmeth($rm) :
> $self->$rmeth() };
> +        die "Error executing run mode '$rm': $@" if $@;
> +    }
>
>      # Make sure that $body is not undefined (supress 'uninitialized
> value' warnings)
>      $body = "" unless defined $body;
> =====

I'll go with option #4.  Best of all worlds.  ;->

my $body = $self->execute_run_mode($rm, $rmeth, $autoload_mode);

[...]

sub execute_run_mode
{
   my ($self, $rm, $rmeth, $autoload_mode) = @_;
   my $body;
   if ($self->can($rmeth)) {
     $body = $autoload_mode ? $self->$rmeth($rm) : $self->$rmeth();
   }
   else {
     $body = eval { $autoload_mode ? $self->$rmeth($rm) : $self->$rmeth(); }
     die "Error executing run mode '$rm': $@" if $@;
   }
   $body;
}


In your application, simply override execute_run_mode:

sub execute_run_mode
{
   my ($self, $rm, $rmeth, $autoload_mode) = @_;
   $autoload_mode ? $self->$rmeth($rm) : $self->$rmeth();
}

Of course, we could take this a small point further, and make the
second execute_run_mode from above into C::A::execute_run_mode_no_catch
and then have an option we can set during setup whether we want C::A to
catch exceptions or not (default to true):

my $exec_func = $self->catch_exceptions() ? \&execute_run_mode :
                \&execute_run_mode_no_catch;
my $body = $self->$exec_func($rm, $rmeth, $autoload_mode);

I'm leaving the creation of sub catch_exceptions as an excersise for
the reader. :-)

I think this allows the most flexibility for everyone.  And where the
above two solutions don't fit someone's desire, they can override
execute_run_mode still.


---------------------------------------------------------------------
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.