blib/lib/Dancer/Error.pm

filter: Statementstmt 95.8% 162 / 169 Branchbran 86.2% 50 / 58 Conditioncond 68.7% 11 / 16 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 96.9% 32 / 33 Podpod 91.6% 11 / 12 totaltotal 90.9% 271 / 298 SCAR 36.3
CC
37
Cov
92%
backtrace
11.2
_censor
8.3
_render_serialized
6.2
CRAP 37.8
« blib/lib/Dancer/Serializer/XML.pm ↑ Summary blib/lib/Dancer/Logger/File.pm »
1 package Dancer::Error;
2 180 our $AUTHORITY = 'cpan:SUKRIA';
3 #ABSTRACT: class for representing fatal errors
4 180 $Dancer::Error::VERSION = '1.3520';
5 180 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 180 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 180 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 180 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 180 ▶use base 'Dancer::Object';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 180 ▶use Dancer::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 180 ▶use Dancer::Renderer;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 180 ▶use Dancer::Config 'setting';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 180 ▶use Dancer::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 180 ▶use Dancer::Factory::Hook;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 180 ▶use Dancer::Session;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 180 ▶use Dancer::FileUtils qw(open_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 180 ▶use Dancer::Engine;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 180 ▶use Dancer::Exception qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 180 Dancer::Factory::Hook->instance->install_hooks(
23 qw/before_error_render after_error_render before_error_init/);
24  
25 sub init {
26 55 ▶ my ($self) = @_;
Subroutine init: called CC=2 SCAR=6.9
Pod documented
27  
28 55 Dancer::Factory::Hook->instance->execute_hooks('before_error_init', $self);
29  
30 55 $self->attributes_defaults(
31 title => 'Error ' . $self->code,
32 type => 'runtime error',
33 );
34  
35 55 ▶ $self->has_serializer
Branch: 1 branch
BranchTrueFalseTotal
$self->has_serializer and return 7 48 55
36 and return;
37  
38 48 my $html_output = "<h2>" . $self->{type} . "</h2>";
39 48 $html_output .= $self->backtrace;
40 48 $html_output .= $self->environment;
41  
42 48 $self->{message} = $html_output;
43 }
44  
45 55 ▶sub has_serializer { setting('serializer') }
Subroutine has_serializer: called CC=1 SCAR=0.0
Pod undocumented
46 162 ▶sub code { $_[0]->{code} }
Subroutine code: called CC=1 SCAR=0.0
Pod documented
47 63 ▶sub title { $_[0]->{title} }
Subroutine title: called CC=1 SCAR=0.0
Pod documented
48 76 ▶sub message { $_[0]->{message} }
Subroutine message: called CC=1 SCAR=0.0
Pod documented
49 96 ▶sub exception { $_[0]->{exception} }
Subroutine exception: called CC=1 SCAR=0.0
Pod documented
50  
51 sub backtrace {
52 48 ▶ my ($self) = @_;
Subroutine backtrace: called CC=11 SCAR=24.1
Pod documented
53  
54 48 ▶ $self->{message} ||= "";
Condition100%2/2
$self->{"message"} ||= ""
l!l
462
MC/DC100%1/1
$self->{"message"} ||= ""
$self->{"message"}
Truth table100%2/2
A
A= $self->{"message"}
Aresult
11
00
55 my $message =
56 48 qq|<pre class="error">| . _html_encode($self->{message}) . "</pre>";
57  
58 # the default perl warning/error pattern
59 48 my ($file, $line) = ($message =~ /at (\S+) line (\d+)/);
60  
61 # the Devel::SimpleTrace pattern
62 48 ▶ ($file, $line) = ($message =~ /at.*\((\S+):(\d+)\)/)
Branch: 1 branch
BranchTrueFalseTotal
unless $file and $line 12 36 48
Condition66%2/3
$file and $line
!ll&&!rl&&r
12036
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 48 ▶ return $message unless ($file and $line);
Branch: 1 branch
BranchTrueFalseTotal
unless $file and $line 12 36 48
Condition66%2/3
$file and $line
!ll&&!rl&&r
12036
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 144 ▶ my $obj = shift;
Subroutine dumper: called CC=6 SCAR=18.1
Pod documented
118 144 ▶ return "Unavailable without Data::Dumper"
Branch: 1 branch
BranchTrueFalseTotal
unless "Dancer::ModuleLoader"->load("Data::Dumper") 0 144 144
119 unless Dancer::ModuleLoader->load('Data::Dumper');
120  
121  
122 # Take a copy of the data, so we can mask sensitive-looking stuff:
123 144 ▶ my $data = Dancer::ModuleLoader->load('Clone') ?
Branch: 1 branch
BranchTrueFalseTotal
'Dancer::ModuleLoader'->load('Clone') ? : 144 0 144
124 Clone::clone($obj)
125 : eval Data::Dumper->new([$obj])->Purity(1)->Terse(1)->Deepcopy(1)->Dump;
126  
127 144 ▶ $data = {%$data} if blessed($data);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($data) 48 96 144
128  
129 144 my $censored = _censor($data);
130  
131 #use Data::Dumper;
132 144 my $dd = Data::Dumper->new([$data]);
133 144 $dd->Terse(1)->Quotekeys(0)->Indent(1)->Sortkeys(1);
134 144 my $content = $dd->Dump();
135 144 $content =~ s{(\s*)(\S+)(\s*)=>}{$1<span class="key">$2</span>$3 =&gt;}g;
136 144 ▶ if ($censored) {
Branch: 1 branch
BranchTrueFalseTotal
if ($censored) 47 97 144
137 47 ▶ $content
Branch: 1 branch
BranchTrueFalseTotal
$censored == 1 ? : 0 47 47
138 .= "\n\nNote: Values of $censored sensitive-looking key"
139 . ($censored == 1 ? '' : 's')
140 . " hidden\n";
141 }
142 144 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 391 ▶ my ( $hash, $recursecount ) = @_;
Subroutine _censor: called CC=8 SCAR=21.1
149 391 ▶ $recursecount ||= 0;
Condition100%2/2
$recursecount ||= 0
l!l
246145
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 180 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
153  
154 391 ▶ if ( $recursecount++ > 100 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recursecount++ > 100) 1 390 391
155 1 warn "Data exceeding 100 levels, truncating\n";
156 1 return $hash;
157 }
158  
159 390 ▶ if (!$hash || ref $hash ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $hash or ref $hash ne "HASH") 0 390 390
Condition33%1/3
not $hash or ref $hash ne "HASH"
l!l&&r!l&&!r
00390
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 390 my $censored = 0;
165 390 for my $key (keys %$hash) {
166 3228 ▶ if (ref $hash->{$key} eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $hash->{$key} eq 'HASH') { } 246 2982 3228
elsif ($key =~ /(pass|card?num|pan|cvv2?|ccv|secret|private_key|cookie_key)/i) { } 94 2888 2982
167 246 $censored += _censor( $hash->{$key}, $recursecount );
168 }
169 elsif ($key =~ /(pass|card?num|pan|cvv2?|ccv|secret|private_key|cookie_key)/i) {
170 94 $hash->{$key} = "Hidden (looks potentially sensitive)";
171 94 $censored++;
172 }
173 }
174  
175 390 return $censored;
176 }
177  
178 # Replaces the entities that are illegal in (X)HTML.
179 sub _html_encode {
180 300 ▶ my $value = shift;
Subroutine _html_encode: called CC=1 SCAR=0.0
181  
182 300 $value =~ s/&/&amp;/g;
183 300 $value =~ s/</&lt;/g;
184 300 $value =~ s/>/&gt;/g;
185 300 $value =~ s/'/&#39;/g;
186 300 $value =~ s/"/&quot;/g;
187  
188 300 return $value;
189 }
190  
191 sub render {
192 53 ▶ my $self = shift;
Subroutine render: called CC=2 SCAR=7.1
Pod documented
193  
194 53 my $serializer = setting('serializer');
195 53 ▶ my $ops = { title => $self->title,
Branch: 1 branch
BranchTrueFalseTotal
defined $self->exception ? : 32 21 53
196 message => $self->message,
197 code => $self->code,
198 defined $self->exception ? ( exception => $self->exception ) : (),
199 };
200 53 Dancer::Factory::Hook->instance->execute_hooks('before_error_render', $self, $ops);
201 53 my $response;
202 try {
203 53 ▶ $response = $serializer ? $self->_render_serialized($ops) : $self->_render_html($ops);
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$serializer ? : 7 46 53
204 } continuation {
205 0 ▶ my ($continuation) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
206 # If we have a Route continuation, run the after hook, then
207 # propagate the continuation
208 0 Dancer::Factory::Hook->instance->execute_hooks('after_error_render', $response);
209 0 $continuation->rethrow();
210 53 };
211 53 Dancer::Factory::Hook->instance->execute_hooks('after_error_render', $response);
212 53 $response;
213 }
214  
215 sub _render_serialized {
216 7 ▶ my $self = shift;
Subroutine _render_serialized: called CC=6 SCAR=18.3
217  
218 7 ▶ my $message =
Branch: 1 branch
BranchTrueFalseTotal
!ref($self->message) ? : 4 3 7
219 !ref $self->message ? {error => $self->message} : $self->message;
220  
221 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
222 2 ▶ if (blessed($self->exception)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self->exception)) { } 0 2 2
223 0 $message->{exception} = ref($self->exception);
224 0 $message->{exception} =~ s/^Dancer::Exception:://;
225 } else {
226 2 $message->{exception} = $self->exception;
227 }
228 }
229  
230 7 ▶ if (setting('show_errors')) {
Branch: 1 branch
BranchTrueFalseTotal
if (setting('show_errors')) { } 6 1 7
231 6 Dancer::Response->new(
232 status => $self->code,
233 content => Dancer::Serializer->engine->serialize($message),
234 headers => ['Content-Type' => Dancer::Serializer->engine->content_type]
235 );
236 }
237  
238 # if show_errors is disabled, we don't expose the real error message to the
239 # outside world
240 else {
241 1 Dancer::Response->new(
242 status => $self->code,
243 content => "An internal error occured",
244 );
245 }
246  
247 }
248  
249 sub _render_html {
250 46 ▶ my $self = shift;
Subroutine _render_html: called CC=3 SCAR=11.0
251 46 my $ops = shift;
252
253 # I think it is irrelevant to look into show_errors. In the
254 # template the user can hide them if she desires so.
255 46 ▶ if (setting("error_template")) {
Branch: 1 branch
BranchTrueFalseTotal
if (setting('error_template')) { } 18 28 46
256 18 my $template_name = setting("error_template");
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 28 ▶ return Dancer::Response->new(
Branch: 1 branch
BranchTrueFalseTotal
if setting("show_errors") 9 19 28
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 48 ▶ my ($self) = @_;
Subroutine environment: called CC=3 SCAR=11.0
Pod documented
276  
277 48 my $request = Dancer::SharedData->request;
278 48 my $r_env = {};
279 48 ▶ $r_env = $request->env if defined $request;
Branch: 1 branch
BranchTrueFalseTotal
if defined $request 46 2 48
280  
281 48 my $env =
282 qq|<div class="title">Environment</div><pre class="content">|
283 . dumper($r_env)
284 . "</pre>";
285 48 my $settings =
286 qq|<div class="title">Settings</div><pre class="content">|
287 . dumper(Dancer::Config->settings)
288 . "</pre>";
289 48 my $source =
290 qq|<div class="title">Stack</div><pre class="content">|
291 . $self->get_caller
292 . "</pre>";
293 48 my $session = "";
294  
295 48 ▶ if (setting('session')) {
Branch: 1 branch
BranchTrueFalseTotal
if (setting("session")) 47 1 48
296 47 $session =
297 qq[<div class="title">Session</div><pre class="content">]
298 . dumper(Dancer::Session->get)
299 . "</pre>";
300 }
301 48 return "$source $settings $session $env";
302 }
303  
304 sub get_caller {
305 48 ▶ my ($self) = @_;
Subroutine get_caller: called CC=2 SCAR=6.9
Pod documented
306 48 my @stack;
307  
308 48 my $deepness = 0;
309 48 ▶ while (my ($package, $file, $line) = caller($deepness++)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = caller $deepness++) 633 48 681
310 633 push @stack, "$package in $file l. $line";
311 }
312  
313 48 return join("\n", reverse(@stack));
314 }
315  
316 180 1;
317  
318 __END__
« blib/lib/Dancer/Serializer/XML.pm ↑ Summary blib/lib/Dancer/Logger/File.pm »