Updated doc and provided tests for abort_chain_on_error_fix
Marco Pessotto [Tue, 30 Aug 2016 12:18:43 +0000 (14:18 +0200)]
There is too much copypasta for my liking here, because there is the
same app with just a different setting in all of the 3 test files,
which are basically the same (save for the config), but it does the job.

lib/Catalyst.pm
t/abort-chain-1.t [new file with mode: 0644]
t/abort-chain-2.t [new file with mode: 0644]
t/abort-chain-3.t [new file with mode: 0644]

index e68e18c..48d3456 100644 (file)
@@ -4297,18 +4297,20 @@ value to undef.
 
 C<abort_chain_on_error_fix>
 
-When there is an error in an action chain, the default behavior is to continue
-processing the remaining actions and then catch the error upon chain end.  This
-can lead to running actions when the application is in an unexpected state.  If
-you have this issue, setting this config value to true will promptly exit a
-chain when there is an error raised in any action (thus terminating the chain
-early.)
+Defaults to true.
 
-use like:
+When there is an error in an action chain, the default behavior is to
+abort the processing of the remaining actions to avoid running them
+when the application is in an unexpected state.
 
-    __PACKAGE__->config(abort_chain_on_error_fix => 1);
+Before version 5.90070, the default used to be false. To keep the old
+behaviour, you can explicitely set the value to false. E.g.
+
+    __PACKAGE__->config(abort_chain_on_error_fix => 0);
+
+If this setting is set to false, then the remaining actions are
+performed and the error is caught at the end of the chain.
 
-In the future this might become the default behavior.
 
 =item *
 
diff --git a/t/abort-chain-1.t b/t/abort-chain-1.t
new file mode 100644 (file)
index 0000000..dba8593
--- /dev/null
@@ -0,0 +1,50 @@
+#!perl
+
+use strict;
+use warnings;
+use Test::More tests => 1;
+use HTTP::Request::Common;
+
+BEGIN {
+    package TestApp::Controller::Root;
+    $INC{'TestApp/Controller/Root.pm'} = __FILE__;
+    use Moose;
+    use MooseX::MethodAttributes;
+    extends 'Catalyst::Controller';
+
+    has counter => (is => 'rw', isa => 'Int', default => sub { 0 });
+    sub increment {
+        my $self = shift;
+        $self->counter($self->counter + 1);
+    }
+    sub root :Chained('/') :PathPart('') :CaptureArgs(0) {
+        my ($self, $c, $arg) = @_;
+        die "Died in root";
+    }
+    sub main :Chained('root') :PathPart('') :Args(0) {
+        my ($self, $c, $arg) = @_;
+        $self->increment;
+        die "Died in main";
+    }
+    sub hits :Path('hits') :Args(0) {
+        my ($self, $c, $arg) = @_;
+        $c->response->body($self->counter);
+    }
+    __PACKAGE__->config(namespace => '');
+}
+{
+    package TestApp;
+    $INC{'TestApp.pm'} = __FILE__;
+    use Catalyst;
+    __PACKAGE__->setup;
+}
+
+use Catalyst::Test 'TestApp';
+
+{
+    my $res = request('/');
+}
+{
+    my $res = request('/hits');
+    is $res->content, 0, "main action not touched on crash with no explicit setting";
+}
diff --git a/t/abort-chain-2.t b/t/abort-chain-2.t
new file mode 100644 (file)
index 0000000..370868c
--- /dev/null
@@ -0,0 +1,51 @@
+#!perl
+
+use strict;
+use warnings;
+use Test::More tests => 1;
+use HTTP::Request::Common;
+
+BEGIN {
+    package TestApp::Controller::Root;
+    $INC{'TestApp/Controller/Root.pm'} = __FILE__;
+    use Moose;
+    use MooseX::MethodAttributes;
+    extends 'Catalyst::Controller';
+
+    has counter => (is => 'rw', isa => 'Int', default => sub { 0 });
+    sub increment {
+        my $self = shift;
+        $self->counter($self->counter + 1);
+    }
+    sub root :Chained('/') :PathPart('') :CaptureArgs(0) {
+        my ($self, $c, $arg) = @_;
+        die "Died in root";
+    }
+    sub main :Chained('root') :PathPart('') :Args(0) {
+        my ($self, $c, $arg) = @_;
+        $self->increment;
+        die "Died in main";
+    }
+    sub hits :Path('hits') :Args(0) {
+        my ($self, $c, $arg) = @_;
+        $c->response->body($self->counter);
+    }
+    __PACKAGE__->config(namespace => '');
+}
+{
+    package TestApp;
+    $INC{'TestApp.pm'} = __FILE__;
+    use Catalyst;
+    __PACKAGE__->config(abort_chain_on_error_fix => 1);
+    __PACKAGE__->setup;
+}
+
+use Catalyst::Test 'TestApp';
+
+{
+    my $res = request('/');
+}
+{
+    my $res = request('/hits');
+    is $res->content, 0, "main action not touched on crash with explicit setting to true";
+}
diff --git a/t/abort-chain-3.t b/t/abort-chain-3.t
new file mode 100644 (file)
index 0000000..1b0f928
--- /dev/null
@@ -0,0 +1,51 @@
+#!perl
+
+use strict;
+use warnings;
+use Test::More tests => 1;
+use HTTP::Request::Common;
+
+BEGIN {
+    package TestApp::Controller::Root;
+    $INC{'TestApp/Controller/Root.pm'} = __FILE__;
+    use Moose;
+    use MooseX::MethodAttributes;
+    extends 'Catalyst::Controller';
+
+    has counter => (is => 'rw', isa => 'Int', default => sub { 0 });
+    sub increment {
+        my $self = shift;
+        $self->counter($self->counter + 1);
+    }
+    sub root :Chained('/') :PathPart('') :CaptureArgs(0) {
+        my ($self, $c, $arg) = @_;
+        die "Died in root";
+    }
+    sub main :Chained('root') :PathPart('') :Args(0) {
+        my ($self, $c, $arg) = @_;
+        $self->increment;
+        die "Died in main";
+    }
+    sub hits :Path('hits') :Args(0) {
+        my ($self, $c, $arg) = @_;
+        $c->response->body($self->counter);
+    }
+    __PACKAGE__->config(namespace => '');
+}
+{
+    package TestApp;
+    $INC{'TestApp.pm'} = __FILE__;
+    use Catalyst;
+    __PACKAGE__->config(abort_chain_on_error_fix => 0);
+    __PACKAGE__->setup;
+}
+
+use Catalyst::Test 'TestApp';
+
+{
+    my $res = request('/');
+}
+{
+    my $res = request('/hits');
+    is $res->content, 1, "main action performed on crash with explicit setting to false";
+}