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
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@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@perldancer.org http://www.backup-manager.org/cgi-bin/listinfo/dancer-users
participants (2)
-
Flavio Poletti -
Taric Mirza