From: Dimitar Petrov Date: Tue, 14 May 2013 10:27:05 +0000 (+0200) Subject: Add option to break a chain if error occurs X-Git-Tag: 5.90040~3^2~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=569b665ec6e28d629c114a5fe34894ad71e8e588 Add option to break a chain if error occurs Signed-off-by: John Napiorkowski --- diff --git a/lib/Catalyst/ActionChain.pm b/lib/Catalyst/ActionChain.pm index 496000b..b54d553 100644 --- a/lib/Catalyst/ActionChain.pm +++ b/lib/Catalyst/ActionChain.pm @@ -34,6 +34,9 @@ sub dispatch { } local $c->request->{arguments} = \@args; $action->dispatch( $c ); + + # break the chain if exception occurs in the middle of chain + return if (@{$c->error} && $c->config->{detach_on_die}); } $last->dispatch( $c ); } diff --git a/t/aggregate/live_component_controller_action_chained.t b/t/aggregate/live_component_controller_action_chained.t index d6fcfed..221169a 100644 --- a/t/aggregate/live_component_controller_action_chained.t +++ b/t/aggregate/live_component_controller_action_chained.t @@ -773,6 +773,25 @@ sub run_tests { } # + # + # + { + my @expected = qw[ + TestApp::Controller::Action::Chained->begin + TestApp::Controller::Action::Chained->chain_die_a + TestApp::Controller::Action::Chained->end + ]; + + my $expected = join( ", ", @expected ); + + ok( my $response = request('http://localhost/chained/chain_die/1/end/2'), + "Break a chain in the middle" ); + is( $response->header('X-Catalyst-Executed'), + $expected, 'Executed actions' ); + is( $response->content, 'FATAL ERROR: break in the middle of a chain', 'Content OK' ); + } + + # # Tests that an uri_for to a chained root index action # returns the right value. # diff --git a/t/lib/TestApp.pm b/t/lib/TestApp.pm index a99301c..9fd0239 100644 --- a/t/lib/TestApp.pm +++ b/t/lib/TestApp.pm @@ -49,7 +49,8 @@ TestApp->config( action_args => { action_action_nine => { another_extra_arg => 13 } } - } + }, + detach_on_die => 1, ); # Test bug found when re-adjusting the metaclass compat code in Moose diff --git a/t/lib/TestApp/Controller/Action/Chained.pm b/t/lib/TestApp/Controller/Action/Chained.pm index 5fa5f22..2af1ec6 100644 --- a/t/lib/TestApp/Controller/Action/Chained.pm +++ b/t/lib/TestApp/Controller/Action/Chained.pm @@ -143,6 +143,15 @@ sub chain_dt_a :Chained :PathPart('chained/chain_dt') :CaptureArgs(1) { sub chain_dt_b :Chained('chain_dt_a') :PathPart('end') :Args(1) { } # +# Die in the middle of a chain +# +sub chain_die_a :Chained :PathPart('chained/chain_die') :CaptureArgs(1) { + $_[1]->error( 'break in the middle of a chain' ); +} + +sub chain_die_b :Chained('chain_die_a') :PathPart('end') :Args(1) {} + +# # Target for former forward and chain tests. # sub fw_dt_target :Private { }