ErrorHandler.pm

filter: Statementstmt 92.6% 138 / 149 Branchbran 73.3% 44 / 60 Conditioncond 63.6% 14 / 22 MC/DCmcdc 42.8% 6 / 14 Subroutinesub 100% 17 / 17 Podpod 100% 4 / 4 totaltotal 83.8% 223 / 266 CC 44 SCAR 20.9
CC
44
Cov
85%
handle
15.0
_parse_actions
10.4
render
7.8
CRAP 8.1
↑ Summary ErrorHandler/Action.pm »
1 package Catalyst::Action::RenderView::ErrorHandler;
2 {
3 6 $Catalyst::Action::RenderView::ErrorHandler::VERSION = '0.100166';
4 }
5 # ABSTRACT: Custom errorhandling in deployed applications
6  
7 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use MRO::Compat;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 ▶use Class::Inspector;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 6 extends 'Catalyst::Action::RenderView';
17  
18 4 ▶has 'handlers' => (is => 'rw', isa => 'ArrayRef', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
19 4 ▶has 'actions' => (is => 'rw', isa => 'HashRef', default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
20  
21 23 ▶sub action {
Subroutine action: called CC=1 SCAR=0.0
Pod documented
22 23 my $self = shift;
23 23 my $id = shift;
24 23 return $self->actions->{$id};
25 }
26 14 ▶sub execute {
Subroutine execute: called CC=5 SCAR=16.1
Pod documented
27 14 my $self = shift;
28 14 my ($controller, $c) = @_;
29  
30 14 my $rv = $self->maybe::next::method(@_);
31 14 ▶ return 1 unless (scalar(@{ $c->error }) or $c->res->status =~ /^4\d\d/);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$c->error;} or $c->res->status =~ /^4\d\d/ 4 10 14
Condition66%2/3
scalar @{$c->error;} or $c->res->status =~ /^4\d\d/
l!l&&r!l&&!r
1004
MC/DC50%1/2
scalar @{$c->error;} or $c->res->status =~ /^4\d\d/
scalar @{$c->error;} $c->res->status =~ /^4\d\d/
Truth table66%2/3
A or B
A= scalar @{$c->error;}B= $c->res->status =~ /^4\d\d/
ABresult
1X1
011
000
32 10 ▶ return 1 if ($c->debug and !$c->config->{'error_handler'}->{'enable'});
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug and not $c->config->{"error_handler"}{"enable"} 1 9 10
Condition100%3/3
$c->debug and not $c->config->{"error_handler"}{"enable"}
!ll&&!rl&&r
721
MC/DC100%2/2
$c->debug and not $c->config->{"error_handler"}{"enable"}
$c->debug not $c->config->{"error_handler"}{"enable"}
Truth table100%3/3
A and not(B)
A= $c->debugB= not $c->config->{"error_handler"}{"enable"}
ABresult
0X0
100
111
33 9 $self->actions({});
34 9 $self->handlers([]);
35 9 $self->_parse_config($c);
36 9 $self->handle($c);
37 }
38  
39 9 ▶sub handle {
Subroutine handle: called CC=14 SCAR=27.1
Pod documented
40 9 my $self = shift;
41 9 my $c = shift;
42  
43 9 my $code = $c->res->status;
44 9 my $has_errors = scalar( @{ $c->error } );
45 9 ▶ if ($code == 200 and $has_errors) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 200 and $has_errors) 3 6 9
Condition66%2/3
$code == 200 and $has_errors
!ll&&!rl&&r
603
MC/DC50%1/2
$code == 200 and $has_errors
$code == 200 $has_errors
Truth table66%2/3
A and B
A= $code == 200B= $has_errors
ABresult
0X0
100
111
46 3 $code = 500; # We default to 500 for errors unless something else has been set.
47 3 $c->res->status($code);
48 }
49 9 my $body;
50 9 foreach my $h (@{ $self->handlers }) {
51 21 ▶ if ($code =~ $h->{decider}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code =~ /$h->{"decider"}/) 9 12 21
52 9 eval {
53 9 $body = $self->render($c, $h->{render});
54 };
55 9 ▶ if ($@ and $@ =~ m/Error rendering/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@ and $@ =~ /Error rendering/) { } 0 9 9
elsif ($@) { } 0 9 9
Condition33%1/3
$@ and $@ =~ /Error rendering/
!ll&&!rl&&r
900
MC/DC0%0/2
$@ and $@ =~ /Error rendering/
$@ $@ =~ /Error rendering/
Truth table33%1/3
A and B
A= $@B= $@ =~ /Error rendering/
ABresult
0X0
100
111
56 # we continue to next step
57 0 next;
58 } elsif ($@) {
59 0 croak $@;
60 }
61 # We have successfully rendered something, so we clear errors
62 # and set content
63 9 $c->res->body($body);
64 9 ▶ if($h->{actions}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($h->{"actions"}) 9 0 9
65 9 foreach my $a (@{ $h->{actions} }) {
66 6 ▶ next unless defined $a;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a 2 4 6
67 4 ▶ next if $a->ignore($c->req->path);
Branch: 1 branch
BranchTrueFalseTotal
if $a->ignore($c->req->path) 1 3 4
68 3 $a->perform($c);
69 }
70 }
71 9 $c->clear_errors;
72  
73 # We have some actions to perform
74 9 last ;
75 }
76 }
77 9 ▶ if ($has_errors and $code =~ m/[23]\d{2}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_errors and $code =~ /[23]\d{2}/) 1 8 9
Condition66%2/3
$has_errors and $code =~ /[23]\d{2}/
!ll&&!rl&&r
081
MC/DC50%1/2
$has_errors and $code =~ /[23]\d{2}/
$has_errors $code =~ /[23]\d{2}/
Truth table66%2/3
A and B
A= $has_errorsB= $code =~ /[23]\d{2}/
ABresult
0X0
100
111
78 1 $c->res->status(500); # We overwrite 2xx and 3xx with 500 if we had errors
79 }
80 }
81 9 ▶sub render {
Subroutine render: called CC=7 SCAR=20.5
Pod documented
82 9 my $self = shift;
83 9 my $c = shift;
84 9 my $args = shift;
85  
86 9 ▶ if ($args->{static}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args->{'static'}) { } 4 5 9
elsif ($args->{'template'}) { } 5 0 5
87 my $file = ($args->{static} !~ m|^/|)
88 ? $c->path_to($args->{static})
89 : $args->{static}
90 4 ▶ ;
Branch: 1 branch
BranchTrueFalseTotal
!($args->{'static'} =~ m[^/]) ? : 4 0 4
91 4 ▶ open(my $fh, "<", $file) or croak "cannot read: $file";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or croak("cannot read: $file") 4 0 4
92 4 return $fh;
93 } elsif ($args->{template}) {
94 # We try to render it using the view, but will catch errors we hope
95 5 my $content;
96 5 my %data = $self->_render_stash( $c );
97 5 ▶ $data{additional_template_paths} ||= [ $c->path_to('root') ]; #compatible w/ earlier version
Condition50%1/2
$data{"additional_template_paths"} ||= [$c->path_to("root")]
l!l
05
MC/DC0%0/1
$data{"additional_template_paths"} ||= [$c->path_to("root")]
$data{"additional_template_paths"}
Truth table50%1/2
A
A= $data{"additional_template_paths"}
Aresult
11
00
98 5 eval {
99 5 $content = $c->view->render( $c, $args->{template}, \%data );
100 };
101 5 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 5 5
102 5 return $content;
103 } else {
104 0 croak "Error rendering - TT error on template " . $args->{template};
105 }
106 } else {
107 0 croak "Error rendering - no template or static";
108 }
109 }
110  
111  
112 5 ▶sub _render_stash {
Subroutine _render_stash: called CC=6 SCAR=18.0
113 5 my $self = shift;
114 5 my $c = shift;
115 5 ▶ return () if !exists $c->config->{'error_handler'}->{'expose_stash'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $c->config->{"error_handler"}{"expose_stash"} 0 5 5
116 5 my $es = $c->config->{'error_handler'}->{'expose_stash'};
117 5 ▶ return map { $_ =~ $es ? ($_ => $c->stash->{$_}) : () } keys %{$c->stash}
Branch: 2 branches
BranchTrueFalseTotal
if ref $es eq "Regexp" 1 4 5
$_ =~ /$es/ ? : 1 1 2
118 if ( ref($es) eq 'Regexp' );
119 4 ▶ return map { $_ => $c->stash->{$_} } @{$es} if ( ref($es) eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $es eq "ARRAY" 1 3 4
120 3 ▶ return ( $es => $c->stash->{$es} ) if ( !ref($es) );
Branch: 1 branch
BranchTrueFalseTotal
if not ref $es 3 0 3
121 }
122  
123 9 ▶sub _parse_config {
Subroutine _parse_config: called CC=1 SCAR=0.0
124 9 my $self = shift;
125 9 my $c = shift;
126  
127 9 $self->_parse_actions($c, $c->config->{'error_handler'}->{'actions'});
128 9 $self->_parse_handlers($c, $c->config->{'error_handler'}->{'handlers'});
129 }
130  
131 9 ▶sub _parse_actions {
Subroutine _parse_actions: called CC=9 SCAR=23.4
132 9 my $self = shift;
133 9 my $c = shift;
134  
135 9 my $actions = shift;
136 9 ▶ unless ($actions and scalar(@$actions)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($actions and scalar @$actions) 4 5 9
Condition100%2/2
$actions and scalar @$actions
!ll
45
MC/DC100%1/1
$actions and scalar @$actions
$actions
Truth table100%2/2
A
A= $actions
Aresult
00
11
137 # We dont have any actions, lets create a default log action.
138 4 my $action = {
139 type => 'Log',
140 id => 'default-log-error',
141 level => 'error',
142 };
143 4 push @$actions, $action;
144 }
145 9 foreach my $action (@$actions) {
146 14 $self->_expand($c, $action );
147 14 my $class;
148 14 ▶ if ($action->{'type'} and $action->{'type'} =~ /^\+/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($action->{'type'} and $action->{'type'} =~ /^\+/) { } 0 14 14
elsif ($action->{'type'}) { } 14 0 14
Condition33%1/3
$action->{"type"} and $action->{"type"} =~ /^\+/
!ll&&!rl&&r
0140
MC/DC0%0/2
$action->{"type"} and $action->{"type"} =~ /^\+/
$action->{"type"} $action->{"type"} =~ /^\+/
Truth table33%1/3
A and B
A= $action->{"type"}B= $action->{"type"} =~ /^\+/
ABresult
0X0
100
111
149 0 $class = $action->{'type'};
150 0 $class =~ s/^\+//;
151 } elsif($action->{'type'}) {
152 14 $class = ref($self) . "::Action::" . $action->{'type'};
153 } else {
154 0 croak "No type specified";
155 }
156  
157 14 ▶ unless(Class::Inspector->loaded($class)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("Class::Inspector"->loaded($class)) 3 11 14
158 3 eval "require $class";
159 3 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 3 3
160 0 croak "Could not load '$class': $@";
161 }
162 }
163 14 my $act = $class->new(%$action);
164 14 $self->actions->{$act->id} = $act;
165 }
166 }
167  
168 9 ▶sub _parse_handlers {
Subroutine _parse_handlers: called CC=7 SCAR=20.1
169 9 my $self = shift;
170 9 my $c = shift;
171 9 my $handlers = shift;
172 9 my $codes = {};
173 9 my $blocks = {};
174 9 ▶ my $fallback = {
Branch: 1 branch
BranchTrueFalseTotal
$self->action('default-log-error') ? : 4 5 9
175 render => { static => 'root/static/error.html' },
176 decider => qr/./,
177 actions => [ $self->action('default-log-error') ? $self->action('default-log-error') : undef ]
178 };
179 9 foreach my $status (keys %$handlers) {
180 my $handler = {
181 10 ▶ actions => [map { $self->action($_) } @{ $handlers->{$status}->{actions}}],
Branch: 1 branch
BranchTrueFalseTotal
$handlers->{$status}{'template'} ? : 19 0 19
182 render => ($handlers->{$status}->{template}
183 ? { template => $handlers->{$status}->{template} }
184 : { static => $handlers->{$status}->{static} }
185 19 ),
186 };
187  
188 19 ▶ if ($status =~ m/\dxx/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($status =~ /\dxx/) { } 5 14 19
elsif ($status =~ /\d{3}/) { } 14 0 14
elsif ($status eq 'fallback') { } 0 0 0
189 #codegroup
190 5 my $decider = $status;
191 5 $decider =~ s/x/\\d/g;
192 5 $handler->{decider} = qr/$decider/;
193 5 $blocks->{$status} = $handler;
194 } elsif ($status =~ m/\d{3}/) {
195 14 $handler->{decider} = qr/$status/;
196 14 $codes->{$status} = $handler;
197 } elsif ($status eq 'fallback') {
198 0 $handler->{decider} = qr/./;
199 0 $fallback = $handler;
200 } else {
201 0 carp "Wrong status: $status specified";
202 }
203 }
204 9 my @handlers;
205 9 push(@handlers, values(%$codes), values(%$blocks), $fallback);
206 9 $self->handlers(\@handlers);
207 }
208  
209 14 ▶sub _expand {
Subroutine _expand: called CC=2 SCAR=6.9
210 14 my $self = shift;
211 14 my $c = shift;
212 14 my $h = shift;
213  
214 14 foreach my $k (keys %$h) {
215 47 my $v = $h->{$k};
216 47 my $name = $c->config->{name};
217 47 $v =~ s/__MYAPP__/$name/g;
218 47 $h->{$k} = $v;
219 }
220 }
221 6 1; # Magic true value required at end of module
222  
223 __END__
↑ Summary ErrorHandler/Action.pm »