Add option to break a chain if error occurs
Dimitar Petrov [Tue, 14 May 2013 10:27:05 +0000 (12:27 +0200)]
Signed-off-by: John Napiorkowski <jjnapiork@cpan.org>

lib/Catalyst/ActionChain.pm
t/aggregate/live_component_controller_action_chained.t
t/lib/TestApp.pm
t/lib/TestApp/Controller/Action/Chained.pm

index 496000b..b54d553 100644 (file)
@@ -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 );
 }
index d6fcfed..221169a 100644 (file)
@@ -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.
     #
index a99301c..9fd0239 100644 (file)
@@ -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
index 5fa5f22..2af1ec6 100644 (file)
@@ -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 { }