[Dancer-users] exception handling

Flavio Poletti polettix at gmail.com
Wed Mar 9 17:25:33 CET 2011


Just a thought, completely untested but should give you the idea:

sub trycatch_get {
   my ($route, $try, $catch) = @_;
   get $route => sub {
      try $try   # or maybe   try { $try->() }
      catch {  # centralised catching code here
         my $exception = $_;

         template 'error', {
             'exception' => $exception,
         };
      };
   };
}

trycatch_get '/' => sub {
       my $data = process_data(); # throws Exception::Class objects

       template 'index', {
           'data' => $data,
       };
};


You could also elaborate a plugin from here :-)

Now that I think about it, try to see if someone already made a plugin for
this!

Cheers,

    Flavio.


On Wed, Mar 9, 2011 at 4:43 PM, Taric Mirza <taric at bigchampagne.com> wrote:

> I want to add some custom exception handling code to a dancer (loving it
> so far!) app and am experimenting around.
>
> eg:
>
> use Dancer ':syntax';
> use Try::Tiny;
>
> get '/' => sub {
>    try {
>        my $data = process_data(); # throws Exception::Class objects
>
>        template 'index', {
>            'data' => $data,
>        };
>    }
>    catch {
>        my $exception = $_;
>
>        template 'error', {
>            'exception' => $exception,
>        };
>    };
>
>    return 1;
> };
>
> 1;
>
>
> This works, but I will have to duplicate the exception handling code for
> every route.  If "get" and the other route handlers were normal methods,
> I would be able to subclass Dancer and add the exception handling code
> that way.  But since it is some kind of syntactic sugar, not sure if
> that is easily doable.  Any ideas?
>
> Thanks.
>
> --
> Taric
>
>
> _______________________________________________
> Dancer-users mailing list
> Dancer-users at perldancer.org
> http://www.backup-manager.org/cgi-bin/listinfo/dancer-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.backup-manager.org/pipermail/dancer-users/attachments/20110309/e95d55f3/attachment.htm>


More information about the Dancer-users mailing list