blib/lib/Plack/Middleware/StackTrace/RethrowFriendly.pm

filter: Statementstmt 100% 71 / 71 Branchbran 94.4% 17 / 18 Conditioncond 66.6% 10 / 15 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 100% 18 / 18 Podpod 20.0% 1 / 5 totaltotal 89.6% 121 / 135 CC 16 SCAR 13.7
CC
16
Cov
94%
call
9.0
should_show_trace
4.8
_make_key
3.0
CRAP 3.9
↑ Summary
1 package Plack::Middleware::StackTrace::RethrowFriendly;
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  
5 # core
6 8 ▶use Scalar::Util 'refaddr';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use MIME::Base64 qw(encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # cpan
10 8 ▶use parent qw(Plack::Middleware::StackTrace);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 8 our $VERSION = "0.03";
14  
15 8 ▶sub call {
Subroutine call: called CC=9 SCAR=22.0
Pod documented
16 8 my($self, $env) = @_;
17  
18 8 my ($last_key, %seen);
19 12 ▶ local $SIG{__DIE__} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
20 12 my $key = _make_key($_[0]);
21 12 ▶ my $list = $seen{$key} || [];
Condition100%2/2
$seen{$key} || []
l!l
210
MC/DC100%1/1
$seen{$key} || []
$seen{$key}
Truth table100%2/2
A
A= $seen{$key}
Aresult
11
00
22  
23 # If we get the same keys, the exception may be rethrown and
24 # we keep the original stacktrace.
25 12 push @$list, $Plack::Middleware::StackTrace::StackTraceClass->new(
26 indent => 1,
27 message => munge_error($_[0], [ caller ]),
28 ignore_package => __PACKAGE__,
29 no_refs => 1,
30 );
31 12 $seen{$key} = $list;
32 12 $last_key = $key;
33  
34 12 die @_;
35 8 };
36  
37 8 my $caught;
38 8 ▶ my $res = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
39 8 $self->app->($env);
40 5 ▶ } catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
41 5 $caught = $_;
42 5 _error('text/plain', $caught, 'no_trace');
43 8 };
44  
45 8 ▶ my $trace = $self->force ? $seen{$last_key} : $seen{_make_key($caught)};
Branch: 1 branch
BranchTrueFalseTotal
$self->force ? : 1 7 8
46 8 ▶ $trace ||= [];
Condition100%2/2
$trace ||= []
l!l
62
MC/DC100%1/1
$trace ||= []
$trace
Truth table100%2/2
A
A= $trace
Aresult
11
00
47 8 ▶ if (scalar @$trace && $self->should_show_trace($caught, $res)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$trace and $self->should_show_trace($caught, $res)) 6 2 8
Condition66%2/3
scalar @$trace and $self->should_show_trace($caught, $res)
!ll&&!rl&&r
206
MC/DC50%1/2
scalar @$trace and $self->should_show_trace($caught, $res)
scalar @$trace $self->should_show_trace($caught, $res)
Truth table66%2/3
A and B
A= scalar @$traceB= $self->should_show_trace($caught, $res)
ABresult
0X0
100
111
48 6 my $text = $trace->[0]->as_string;
49 6 ▶ my $html = @$trace > 1
Branch: 1 branch
BranchTrueFalseTotal
@$trace > 1 ? : 2 4 6
50 ? _multi_trace_html(@$trace) : $trace->[0]->as_html;
51 6 $env->{'plack.stacktrace.text'} = $text;
52 6 $env->{'plack.stacktrace.html'} = $html;
53 6 ▶ $env->{'psgi.errors'}->print($text) unless $self->no_print_errors;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->no_print_errors 0 6 6
54 6 ▶ $res = ($env->{HTTP_ACCEPT} || '*/*') =~ /html/
Branch: 1 branch
BranchTrueFalseTotal
($env->{'HTTP_ACCEPT'} || '*/*') =~ /html/ ? : 1 5 6
Condition100%2/2
$env->{'HTTP_ACCEPT'} || '*/*'
l!l
15
MC/DC100%1/1
$env->{'HTTP_ACCEPT'} || '*/*'
$env->{'HTTP_ACCEPT'}
Truth table100%2/2
A
A= $env->{'HTTP_ACCEPT'}
Aresult
11
00
55 ? _error('text/html', $html)
56 : _error('text/plain', $text);
57 }
58 # break %seen here since $SIG{__DIE__} holds the ref to it, and
59 # $trace has refs to Standalone.pm's args ($conn etc.) and
60 # prevents garbage collection to be happening.
61 8 undef %seen;
62  
63 8 return $res;
64 }
65  
66 6 ▶sub should_show_trace {
Subroutine should_show_trace: called CC=4 SCAR=15.6
Pod undocumented
67 6 my ($self, $err, $res) = @_;
68 6 ▶ return 1 if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 5 1 6
69 1 ▶ return $self->force && ref $res eq 'ARRAY' && $res->[0] == 500;
Condition33%2/6
$self->force && ref $res eq 'ARRAY' && $res->[0] == 500
!ll&&!rl&&r
001
$self->force and ref $res eq "ARRAY"
!ll&&!rl&&r
001
MC/DC0%0/3
$self->force && ref $res eq 'ARRAY' && $res->[0] == 500
$self->force ref $res eq "ARRAY" $res->[0] == 500
Truth table25%1/4
A and B && C
A= $self->forceB= ref $res eq "ARRAY"C= $res->[0] == 500
ABCresult
0XX0
10X0
1100
1111
70 }
71  
72 5 ▶sub no_trace_error { Plack::Middleware::StackTrace::no_trace_error(@_) }
Subroutine no_trace_error: called CC=1 SCAR=0.0
Pod undocumented
73 12 ▶sub munge_error { Plack::Middleware::StackTrace::munge_error(@_) }
Subroutine munge_error: called CC=1 SCAR=0.0
Pod undocumented
74 11 ▶sub utf8_safe { Plack::Middleware::StackTrace::utf8_safe(@_) }
Subroutine utf8_safe: called CC=1 SCAR=0.0
Pod undocumented
75  
76 19 ▶sub _make_key {
Subroutine _make_key: called CC=3 SCAR=11.0
77 19 my ($val) = @_;
78 19 ▶ if (!defined($val)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $val) { } 2 17 19
elsif (ref $val) { } 6 11 17
79 2 return 'undef';
80 } elsif (ref($val)) {
81 6 return 'ref:' . refaddr($val);
82 } else {
83 11 return "str:$val";
84 }
85 }
86  
87 11 ▶sub _error {
Subroutine _error: called CC=2 SCAR=6.9
88 11 my ($type, $content, $no_trace) = @_;
89 11 $content = utf8_safe($content);
90 11 ▶ $content = no_trace_error($content) if $no_trace;
Branch: 1 branch
BranchTrueFalseTotal
if $no_trace 5 6 11
91 11 return [ 500, [ 'Content-Type' => "$type; charset=utf-8" ], [ $content ] ];
92 }
93  
94 2 ▶sub _multi_trace_html {
Subroutine _multi_trace_html: called CC=1 SCAR=0.0
95 2 my (@trace) = @_;
96  
97 2 require Devel::StackTrace::AsHTML;
98 2 my $msg = Devel::StackTrace::AsHTML::encode_html(
99 $trace[0]->frame(0)->as_string(1),
100 );
101  
102 2 my @data_uris = map { _trace_as_data_uri($_) } @trace;
103 my @links = map {
104 2 sprintf
105 '<a class="link" href="%s" target="trace">#%s</a>',
106 $data_uris[$_],
107 $_;
108 } (0..$#data_uris);
109  
110 2 my $css = << '----';
111 body {
112 height: 100%;
113 margin: 0;
114 padding: 0;
115 }
116 div#links {
117 z-index: 100;
118 position: absolute;
119 top: 0;
120 height: 30px;
121 }
122 #content {
123 z-index: 50;
124 position: absolute;
125 top: 0;
126 height: 100%;
127 width: 100%;
128 margin: 0;
129 padding: 30px 0 4px 0;
130 box-sizing: border-box;
131 }
132 iframe#trace {
133 border: none;
134 padding: 0;
135 margin: 0;
136 height: 100%;
137 width: 100%;
138 }
139 a.selected {
140 color: #000;
141 font-weight: bold;
142 text-decoration: none;
143 }
144 ----
145  
146 2 sprintf << '----', $msg, $css, join(' ', @links), $data_uris[0];
147 <!DOCTYPE html>
148 <html>
149 <head>
150 <title>Error: %s</title>
151 <style type="text/css">
152 %s
153 </style>
154 <script>
155 (function(d) {
156 var select = function() {
157 Array.prototype.forEach.call(d.querySelectorAll('a.link'), function(a) {
158 a.className = a.className.replace(/\s*\bselected\b/, '');
159 });
160 this.className += ' selected';
161 };
162 d.addEventListener('DOMContentLoaded', function() {
163 Array.prototype.forEach.call(d.querySelectorAll('a.link'), function(a) {
164 a.addEventListener('click', select);
165 });
166 select.call(d.querySelector('a.link'));
167 });
168 })(document);
169 </script>
170 </head>
171 <body>
172 <div id="links">
173 Throws: %s
174 </div>
175 <div id="content">
176 <iframe src="%s" id="trace" name="trace"></iframe>
177 </div>
178 </body>
179 </html>
180 ----
181 }
182  
183 4 ▶sub _trace_as_data_uri ($) {
Subroutine _trace_as_data_uri: called CC=1 SCAR=0.0
184 4 my $html = $_[0]->as_html;
185 4 return 'data:text/html;charset=utf-8;base64,'.encode_base64($html);
186 }
187  
188 8 1;
189 __END__
↑ Summary