blib/lib/Dancer/Error.pm

filter: Statementstmt 95.8% 161 / 168 Branchbran 84.4% 49 / 58 Conditioncond 68.7% 11 / 16 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 96.9% 32 / 33 Podpod 91.6% 11 / 12 totaltotal 90.5% 269 / 297 CC 37 SCAR 15.6
CC
37
Cov
91%
backtrace
11.2
_censor
8.3
_render_serialized
6.2
CRAP 4.8
« blib/lib/Dancer/Deprecation.pm ↑ Summary blib/lib/Dancer/ModuleLoader.pm »
1 package Dancer::Error;
2 178 our $AUTHORITY = 'cpan:SUKRIA';
3 #ABSTRACT: class for representing fatal errors
4 178 $Dancer::Error::VERSION = '1.3513';
5 178 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 178 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 178 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 178 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 178 ▶use base 'Dancer::Object';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 178 ▶use Dancer::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 178 ▶use Dancer::Renderer;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 178 ▶use Dancer::Config 'setting';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 178 ▶use Dancer::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 178 ▶use Dancer::Factory::Hook;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 178 ▶use Dancer::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 178 ▶use Dancer::FileUtils qw(open_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 178 ▶use Dancer::Engine;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 178 ▶use Dancer::Exception qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 178 Dancer::Factory::Hook->instance->install_hooks(
23 qw/before_error_render after_error_render before_error_init/);
24  
25 sub init {
26 53 ▶ my ($self) = @_;
Subroutine init: called CC=2 SCAR=6.9
Pod documented
27  
28 53 Dancer::Factory::Hook->instance->execute_hooks('before_error_init', $self);
29  
30 53 $self->attributes_defaults(
31 title => 'Error ' . $self->code,
32 type => 'runtime error',
33 );
34  
35 53 ▶ $self->has_serializer
Branch: 1 branch
BranchTrueFalseTotal
$self->has_serializer and return 7 46 53
36 and return;
37  
38 46 my $html_output = "<h2>" . $self->{type} . "</h2>";
39 46 $html_output .= $self->backtrace;
40 46 $html_output .= $self->environment;
41  
42 46 $self->{message} = $html_output;
43 }
44  
45 53 ▶sub has_serializer { setting('serializer') }
Subroutine has_serializer: called CC=1 SCAR=0.0
Pod undocumented
46 123 ▶sub code { $_[0]->{code} }
Subroutine code: called CC=1 SCAR=0.0
Pod documented
47 26 ▶sub title { $_[0]->{title} }
Subroutine title: called CC=1 SCAR=0.0
Pod documented
48 39 ▶sub message { $_[0]->{message} }
Subroutine message: called CC=1 SCAR=0.0
Pod documented
49 47 ▶sub exception { $_[0]->{exception} }
Subroutine exception: called CC=1 SCAR=0.0
Pod documented
50  
51 sub backtrace {
52 46 ▶ my ($self) = @_;
Subroutine backtrace: called CC=11 SCAR=24.1
Pod documented
53  
54 46 ▶ $self->{message} ||= "";
Condition100%2/2
$self->{"message"} ||= ""
l!l
442
MC/DC100%1/1
$self->{"message"} ||= ""
$self->{"message"}
Truth table100%2/2
A
A= $self->{"message"}
Aresult
11
00
55 my $message =
56 46 qq|<pre class="error">| . _html_encode($self->{message}) . "</pre>";
57  
58 # the default perl warning/error pattern
59 46 my ($file, $line) = ($message =~ /at (\S+) line (\d+)/);
60  
61 # the Devel::SimpleTrace pattern
62 46 ▶ ($file, $line) = ($message =~ /at.*\((\S+):(\d+)\)/)
Branch: 1 branch
BranchTrueFalseTotal
unless $file and $line 10 36 46
Condition66%2/3
$file and $line
!ll&&!rl&&r
10036
MC/DC50%1/2
$file and $line
$file $line
Truth table66%2/3
A and B
A= $fileB= $line
ABresult
0X0
100
111
63 unless $file and $line;
64  
65 # no file/line found, cannot open a file for context
66 46 ▶ return $message unless ($file and $line);
Branch: 1 branch
BranchTrueFalseTotal
unless $file and $line 10 36 46
Condition66%2/3
$file and $line
!ll&&!rl&&r
10036
MC/DC50%1/2
$file and $line
$file $line
Truth table66%2/3
A and B
A= $fileB= $line
ABresult
0X0
100
111
67  
68 # file and line are located, let's read the source Luke!
69 36 ▶ my $fh = open_file('<', $file) or return $message;
Branch: 1 branch
BranchTrueFalseTotal
my $fh = open_file("<", $file) or return $message 0 36 36
70 36 my @lines = <$fh>;
71 36 close $fh;
72  
73 36 my $backtrace = $message;
74  
75 36 $backtrace
76 .= qq|<div class="title">| . "$file around line $line" . "</div>";
77  
78 36 $backtrace .= qq|<pre class="content">|;
79  
80 36 $line--;
81 36 ▶ my $start = (($line - 3) >= 0) ? ($line - 3) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$line - 3 >= 0 ? : 36 0 36
82 36 ▶ my $stop = (($line + 3) < scalar(@lines)) ? ($line + 3) : scalar(@lines);
Branch: 1 branch
BranchTrueFalseTotal
$line + 3 < scalar @lines ? : 36 0 36
83  
84 36 ▶ for (my $l = $start; $l <= $stop; $l++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l <= $stop) 252 36 288
85 252 chomp $lines[$l];
86  
87 252 ▶ if ($l == $line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l == $line) { } 36 216 252
88 36 $backtrace
89 .= qq|<span class="nu">|
90 . tabulate($l + 1, $stop + 1)
91 . qq|</span> <span style="color: red;">|
92 . _html_encode($lines[$l])
93 . "</span>\n";
94 }
95 else {
96 216 $backtrace
97 .= qq|<span class="nu">|
98 . tabulate($l + 1, $stop + 1)
99 . "</span> "
100 . _html_encode($lines[$l]) . "\n";
101 }
102 }
103 36 $backtrace .= "</pre>";
104  
105  
106 36 return $backtrace;
107 }
108  
109 sub tabulate {
110 252 ▶ my ($number, $max) = @_;
Subroutine tabulate: called CC=2 SCAR=6.9
Pod documented
111 252 my $len = length($max);
112 252 ▶ return $number if length($number) == $len;
Branch: 1 branch
BranchTrueFalseTotal
if length $number == $len 242 10 252
113 10 return " $number";
114 }
115  
116 sub dumper {
117 138 ▶ my $obj = shift;
Subroutine dumper: called CC=6 SCAR=18.1
Pod documented
118 138 ▶ return "Unavailable without Data::Dumper"
Branch: 1 branch
BranchTrueFalseTotal
unless "Dancer::ModuleLoader"->load("Data::Dumper") 0 138 138
119 unless Dancer::ModuleLoader->load('Data::Dumper');
120  
121  
122 # Take a copy of the data, so we can mask sensitive-looking stuff:
123 138 ▶ my $data = Dancer::ModuleLoader->load('Clone') ?
Branch: 1 branch
BranchTrueFalseTotal
'Dancer::ModuleLoader'->load('Clone') ? : 138 0 138
124 Clone::clone($obj)
125 : eval Data::Dumper->new([$obj])->Purity(1)->Terse(1)->Deepcopy(1)->Dump;
126  
127 138 ▶ $data = {%$data} if blessed($data);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($data) 46 92 138
128  
129 138 my $censored = _censor($data);
130  
131 #use Data::Dumper;
132 138 my $dd = Data::Dumper->new([$data]);
133 138 $dd->Terse(1)->Quotekeys(0)->Indent(1)->Sortkeys(1);
134 138 my $content = $dd->Dump();
135 138 $content =~ s{(\s*)(\S+)(\s*)=>}{$1<span class="key">$2</span>$3 =&gt;}g;
136 138 ▶ if ($censored) {
Branch: 1 branch
BranchTrueFalseTotal
if ($censored) 45 93 138
137 45 ▶ $content
Branch: 1 branch
BranchTrueFalseTotal
$censored == 1 ? : 0 45 45
138 .= "\n\nNote: Values of $censored sensitive-looking key"
139 . ($censored == 1 ? '' : 's')
140 . " hidden\n";
141 }
142 138 return $content;
143 }
144  
145 # Given a hashref, censor anything that looks sensitive. Returns number of
146 # items which were "censored".
147 sub _censor {
148 379 ▶ my ( $hash, $recursecount ) = @_;
Subroutine _censor: called CC=8 SCAR=21.1
149 379 ▶ $recursecount ||= 0;
Condition100%2/2
$recursecount ||= 0
l!l
240139
MC/DC100%1/1
$recursecount ||= 0
$recursecount
Truth table100%2/2
A
A= $recursecount
Aresult
11
00
150  
151 # we're checking recursion ourselves, no need to warn
152 178 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
153  
154 379 ▶ if ( $recursecount++ > 100 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recursecount++ > 100) 1 378 379
155 1 warn "Data exceeding 100 levels, truncating\n";
156 1 return $hash;
157 }
158  
159 378 ▶ if (!$hash || ref $hash ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $hash or ref $hash ne "HASH") 0 378 378
Condition33%1/3
not $hash or ref $hash ne "HASH"
l!l&&r!l&&!r
00378
MC/DC0%0/2
not $hash or ref $hash ne "HASH"
not $hash ref $hash ne "HASH"
Truth table33%1/3
not(A) or B
A= not $hashB= ref $hash ne "HASH"
ABresult
1X1
011
000
160 0 carp "_censor given incorrect input: $hash";
161 0 return;
162 }
163  
164 378 my $censored = 0;
165 378 for my $key (keys %$hash) {
166 3096 ▶ if (ref $hash->{$key} eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $hash->{$key} eq 'HASH') { } 240 2856 3096
elsif ($key =~ /(pass|card?num|pan|cvv2?|ccv|secret|private_key|cookie_key)/i) { } 90 2766 2856
167 240 $censored += _censor( $hash->{$key}, $recursecount );
168 }
169 elsif ($key =~ /(pass|card?num|pan|cvv2?|ccv|secret|private_key|cookie_key)/i) {
170 90 $hash->{$key} = "Hidden (looks potentially sensitive)";
171 90 $censored++;
172 }
173 }
174  
175 378 return $censored;
176 }
177  
178 # Replaces the entities that are illegal in (X)HTML.
179 sub _html_encode {
180 298 ▶ my $value = shift;
Subroutine _html_encode: called CC=1 SCAR=0.0
181  
182 298 $value =~ s/&/&amp;/g;
183 298 $value =~ s/</&lt;/g;
184 298 $value =~ s/>/&gt;/g;
185 298 $value =~ s/'/&#39;/g;
186 298 $value =~ s/"/&quot;/g;
187  
188 298 return $value;
189 }
190  
191 sub render {
192 51 ▶ my $self = shift;
Subroutine render: called CC=1 SCAR=0.1
Pod documented
193  
194 51 my $serializer = setting('serializer');
195 51 Dancer::Factory::Hook->instance->execute_hooks('before_error_render', $self);
196 51 my $response;
197 try {
198 51 ▶ $response = $serializer ? $self->_render_serialized() : $self->_render_html();
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$serializer ? : 7 44 51
199 } continuation {
200 0 ▶ my ($continuation) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
201 # If we have a Route continuation, run the after hook, then
202 # propagate the continuation
203 0 Dancer::Factory::Hook->instance->execute_hooks('after_error_render', $response);
204 0 $continuation->rethrow();
205 51 };
206 51 Dancer::Factory::Hook->instance->execute_hooks('after_error_render', $response);
207 51 $response;
208 }
209  
210 sub _render_serialized {
211 7 ▶ my $self = shift;
Subroutine _render_serialized: called CC=6 SCAR=18.3
212  
213 7 ▶ my $message =
Branch: 1 branch
BranchTrueFalseTotal
!ref($self->message) ? : 4 3 7
214 !ref $self->message ? {error => $self->message} : $self->message;
215  
216 7 ▶ if (ref $message eq 'HASH' && defined $self->exception) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $message eq "HASH" and defined $self->exception) 2 5 7
Condition66%2/3
ref $message eq "HASH" and defined $self->exception
!ll&&!rl&&r
052
MC/DC50%1/2
ref $message eq "HASH" and defined $self->exception
ref $message eq "HASH" defined $self->exception
Truth table66%2/3
A and B
A= ref $message eq "HASH"B= defined $self->exception
ABresult
0X0
100
111
217 2 ▶ if (blessed($self->exception)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self->exception)) { } 0 2 2
218 0 $message->{exception} = ref($self->exception);
219 0 $message->{exception} =~ s/^Dancer::Exception:://;
220 } else {
221 2 $message->{exception} = $self->exception;
222 }
223 }
224  
225 7 ▶ if (setting('show_errors')) {
Branch: 1 branch
BranchTrueFalseTotal
if (setting('show_errors')) { } 6 1 7
226 6 Dancer::Response->new(
227 status => $self->code,
228 content => Dancer::Serializer->engine->serialize($message),
229 headers => ['Content-Type' => Dancer::Serializer->engine->content_type]
230 );
231 }
232  
233 # if show_errors is disabled, we don't expose the real error message to the
234 # outside world
235 else {
236 1 Dancer::Response->new(
237 status => $self->code,
238 content => "An internal error occured",
239 );
240 }
241  
242 }
243  
244 sub _render_html {
245 44 ▶ my $self = shift;
Subroutine _render_html: called CC=4 SCAR=13.9
246  
247 # I think it is irrelevant to look into show_errors. In the
248 # template the user can hide them if she desires so.
249 44 ▶ if (setting("error_template")) {
Branch: 1 branch
BranchTrueFalseTotal
if (setting('error_template')) { } 18 26 44
250 18 my $template_name = setting("error_template");
251 18 ▶ my $ops = {
Branch: 1 branch
BranchTrueFalseTotal
defined $self->exception ? : 18 0 18
252 title => $self->title,
253 message => $self->message,
254 code => $self->code,
255 defined $self->exception ? ( exception => $self->exception ) : (),
256 };
257 18 my $content = Dancer::Engine->engine("template")->apply_renderer($template_name, $ops);
258 18 return Dancer::Response->new(
259 status => $self->code,
260 headers => ['Content-Type' => 'text/html'],
261 content => $content);
262 } else {
263 26 ▶ return Dancer::Response->new(
Branch: 1 branch
BranchTrueFalseTotal
if setting("show_errors") 7 19 26
264 status => $self->code,
265 headers => ['Content-Type' => 'text/html'],
266 content =>
267 Dancer::Renderer->html_page($self->title, $self->message, 'error')
268 ) if setting('show_errors');
269  
270 19 return Dancer::Renderer->render_error($self->code);
271 }
272 }
273  
274 sub environment {
275 46 ▶ my ($self) = @_;
Subroutine environment: called CC=3 SCAR=11.0
Pod documented
276  
277 46 my $request = Dancer::SharedData->request;
278 46 my $r_env = {};
279 46 ▶ $r_env = $request->env if defined $request;
Branch: 1 branch
BranchTrueFalseTotal
if defined $request 44 2 46
280  
281 46 my $env =
282 qq|<div class="title">Environment</div><pre class="content">|
283 . dumper($r_env)
284 . "</pre>";
285 46 my $settings =
286 qq|<div class="title">Settings</div><pre class="content">|
287 . dumper(Dancer::Config->settings)
288 . "</pre>";
289 46 my $source =
290 qq|<div class="title">Stack</div><pre class="content">|
291 . $self->get_caller
292 . "</pre>";
293 46 my $session = "";
294  
295 46 ▶ if (setting('session')) {
Branch: 1 branch
BranchTrueFalseTotal
if (setting("session")) 45 1 46
296 45 $session =
297 qq[<div class="title">Session</div><pre class="content">]
298 . dumper(Dancer::Session->get)
299 . "</pre>";
300 }
301 46 return "$source $settings $session $env";
302 }
303  
304 sub get_caller {
305 46 ▶ my ($self) = @_;
Subroutine get_caller: called CC=2 SCAR=6.9
Pod documented
306 46 my @stack;
307  
308 46 my $deepness = 0;
309 46 ▶ while (my ($package, $file, $line) = caller($deepness++)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = caller $deepness++) 599 46 645
310 599 push @stack, "$package in $file l. $line";
311 }
312  
313 46 return join("\n", reverse(@stack));
314 }
315  
316 178 1;
317  
318 __END__
« blib/lib/Dancer/Deprecation.pm ↑ Summary blib/lib/Dancer/ModuleLoader.pm »