blib/lib/Plack/Middleware/StackTrace.pm

filter: Statementstmt 94.4% 68 / 72 Branchbran 85.0% 17 / 20 Conditioncond 75.0% 15 / 20 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 100% 16 / 16 Podpod 25.0% 1 / 4 totaltotal 84.5% 120 / 142 CC 17 SCAR 17.4
CC
17
Cov
89%
call
12.1
__ANON__
4.1
utf8_safe
2.1
CRAP 5.7
« blib/lib/Plack/App/URLMap.pm ↑ Summary blib/lib/Plack/Middleware/Log4perl.pm »
1 package Plack::Middleware::StackTrace;
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use parent qw/Plack::Middleware/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Devel::StackTrace;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Devel::StackTrace::AsHTML;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Scalar::Util qw( refaddr );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use Plack::Util::Accessor qw( force no_print_errors );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 our $StackTraceClass = "Devel::StackTrace";
12  
13 # Optional since it needs PadWalker
14 0 ▶if (try { require Devel::StackTrace::WithLexicals; Devel::StackTrace::WithLexicals->VERSION(0.08); 1 }) {
Subroutine __ANON__: called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
if (&try(sub { require Devel::StackTrace::WithLexicals; "Devel::StackTrace::WithLexicals"->VERSION("0.08"); 1; } )) 0 8 8
15 0 $StackTraceClass = "Devel::StackTrace::WithLexicals";
16 }
17  
18 10 ▶sub call {
Subroutine call: called CC=12 SCAR=24.9
Pod documented
19 10 my($self, $env) = @_;
20  
21 10 my ($trace, %string_traces, %ref_traces);
22 14 ▶ local $SIG{__DIE__} = sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
23 14 $trace = $StackTraceClass->new(
24 indent => 1, message => munge_error($_[0], [ caller ]),
25 ignore_package => __PACKAGE__, no_refs => 1,
26 );
27 14 ▶ if (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 4 10 14
28 4 ▶ $ref_traces{refaddr($_[0])} ||= $trace;
Condition66%2/3
$ref_traces{builtin::refaddr($_[0])} ||= $trace
l!l&&r!l&&!r
130
MC/DC0%0/2
$ref_traces{builtin::refaddr($_[0])} ||= $trace
$ref_traces{builtin::refaddr($_[0])} $trace
Truth table66%2/3
A ||= B
A= $ref_traces{builtin::refaddr($_[0])}B= $trace
ABresult
1X1
011
000
29 }
30 else {
31 10 ▶ $string_traces{$_[0]} ||= $trace;
Condition66%2/3
$string_traces{$_[0]} ||= $trace
l!l&&r!l&&!r
190
MC/DC0%0/2
$string_traces{$_[0]} ||= $trace
$string_traces{$_[0]} $trace
Truth table66%2/3
A ||= B
A= $string_traces{$_[0]}B= $trace
ABresult
1X1
011
000
32 }
33 14 die @_;
34 10 };
35  
36 10 my $caught;
37 10 ▶ my $res = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
38 10 $self->app->($env);
39 7 ▶ } catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
40 7 $caught = $_;
41 7 [ 500, [ "Content-Type", "text/plain; charset=utf-8" ], [ no_trace_error(utf8_safe($caught)) ] ];
42 10 };
43  
44 10 ▶ if ($caught) {
Branch: 1 branch
BranchTrueFalseTotal
if ($caught) 7 3 10
45 # Try to find the correct trace for the caught exception
46 7 my $caught_trace;
47 7 ▶ if (ref $caught) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $caught) { } 2 5 7
48 2 $caught_trace = $ref_traces{refaddr($caught)};
49 }
50 else {
51 # This is not guaranteed to work if multiple exceptions with
52 # the same message are thrown.
53 5 $caught_trace = $string_traces{$caught};
54 }
55 7 ▶ $trace = $caught_trace if $caught_trace;
Branch: 1 branch
BranchTrueFalseTotal
if $caught_trace 7 0 7
56 }
57  
58 # Use ref $trace to avoid overloaded as_string() for bool evaluation
59 10 ▶ if (ref $trace && ($caught || ($self->force && ref $res eq 'ARRAY' && $res->[0] == 500)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $trace and $caught || $self->force && ref $res eq "ARRAY" && $res->[0] == 500) 8 2 10
Condition75%9/12
ref $trace and $caught || $self->force && ref $res eq "ARRAY" && $res->[0] == 500
!ll&&!rl&&r
028
$caught or $self->force and ref $res eq "ARRAY" and $res->[0] == 500
l!l&&r!l&&!r
712
$self->force and ref $res eq "ARRAY" and $res->[0] == 500
!ll&&!rl&&r
201
$self->force and ref $res eq "ARRAY"
!ll&&!rl&&r
201
MC/DC40%2/5
ref $trace and $caught || $self->force && ref $res eq "ARRAY" && $res->[0] == 500
ref $trace $caught $self->force ref $res eq "ARRAY" $res->[0] == 500
Truth table50%3/6
A and B or C and D and E
A= ref $traceB= $caughtC= $self->forceD= ref $res eq "ARRAY"E= $res->[0] == 500
ABCDEresult
0XXXX0
100XX0
1010X0
101100
11XXX1
101111
60 8 my $text = $trace->as_string;
61 8 my $html = $trace->as_html;
62 8 $env->{'plack.stacktrace.text'} = $text;
63 8 $env->{'plack.stacktrace.html'} = $html;
64 8 ▶ $env->{'psgi.errors'}->print($text) unless $self->no_print_errors;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->no_print_errors 2 6 8
65 8 ▶ if (($env->{HTTP_ACCEPT} || '*/*') =~ /html/) {
Branch: 1 branch
BranchTrueFalseTotal
if (($env->{'HTTP_ACCEPT'} || '*/*') =~ /html/) { } 1 7 8
Condition100%2/2
$env->{'HTTP_ACCEPT'} || '*/*'
l!l
17
MC/DC100%1/1
$env->{'HTTP_ACCEPT'} || '*/*'
$env->{'HTTP_ACCEPT'}
Truth table100%2/2
A
A= $env->{'HTTP_ACCEPT'}
Aresult
11
00
66 1 $res = [500, ['Content-Type' => 'text/html; charset=utf-8'], [ utf8_safe($html) ]];
67 } else {
68 7 $res = [500, ['Content-Type' => 'text/plain; charset=utf-8'], [ utf8_safe($text) ]];
69 }
70 }
71  
72 # break $trace here since $SIG{__DIE__} holds the ref to it, and
73 # $trace has refs to Standalone.pm's args ($conn etc.) and
74 # prevents garbage collection to be happening.
75 10 undef $trace;
76  
77 10 return $res;
78 }
79  
80 7 ▶sub no_trace_error {
Subroutine no_trace_error: called CC=1 SCAR=0.0
Pod undocumented
81 7 my $msg = shift;
82 7 chomp($msg);
83  
84 7 return <<EOF;
85 The application raised the following error:
86  
87 $msg
88  
89 and the StackTrace middleware couldn't catch its stack trace, possibly because your application overrides \$SIG{__DIE__} by itself, preventing the middleware from working correctly. Remove the offending code or module that does it: known examples are CGI::Carp and Carp::Always.
90 EOF
91 }
92  
93 14 ▶sub munge_error {
Subroutine munge_error: called CC=2 SCAR=6.9
Pod undocumented
94 14 my($err, $caller) = @_;
95 14 ▶ return $err if ref $err;
Branch: 1 branch
BranchTrueFalseTotal
if ref $err 4 10 14
96  
97 # Ugly hack to remove " at ... line ..." automatically appended by perl
98 # If there's a proper way to do this, please let me know.
99 10 $err =~ s/ at \Q$caller->[1]\E line $caller->[2]\.\n$//;
100  
101 10 return $err;
102 }
103  
104 15 ▶sub utf8_safe {
Subroutine utf8_safe: called CC=2 SCAR=7.6
Pod undocumented
105 15 my $str = shift;
106  
107 # NOTE: I know messing with utf8:: in the code is WRONG, but
108 # because we're running someone else's code that we can't
109 # guarantee which encoding an exception is encoded, there's no
110 # better way than doing this. The latest Devel::StackTrace::AsHTML
111 # (0.08 or later) encodes high-bit chars as HTML entities, so this
112 # path won't be executed.
113 15 ▶ if (utf8::is_utf8($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (utf8::is_utf8($str)) 0 15 15
114 0 utf8::encode($str);
115 }
116  
117 15 $str;
118 }
119  
120 8 1;
121  
122 __END__
« blib/lib/Plack/App/URLMap.pm ↑ Summary blib/lib/Plack/Middleware/Log4perl.pm »