Cake/Exception.pm

filter: Statementstmt 61.0% 58 / 95 Branchbran 37.5% 12 / 32 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 71.4% 10 / 14 Podpod 0.0% 0 / 8 totaltotal 51.5% 82 / 159 CC 19 SCAR 25.8
CC
19
Cov
54%
error
42.0
backtrace
12.7
warn
6.0
CRAP 13.2
« Cake/Plugins/Simple/Session.pm ↑ Summary Cake/Utils.pm »
1 package Cake::Exception;
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
5
6 8 our @CARP_NOT;
7 8 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 our @EXPORT = qw(
9 error
10 log
11 warn
12 );
13
14 ##For quick testing
15 8 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 $SIG{__DIE__} = \&trapper;
17 }
18
19 6 ▶sub trapper {
Subroutine trapper: called CC=1 SCAR=0.0
Pod undocumented
20 6 my $message = shift;
21 6 $message = __PACKAGE__->backtrace($message);
22 6 print STDOUT <<END;
23 Content-Type: text/html
24 $message
25 END
26 }
27
28 ## die nicely just to detach the flow sequence of Cake action
29 ## and not a real die
30 8 my $kill_nicely = 0;
31 0 ▶sub Mercy_Killing {
Subroutine Mercy_Killing: not called CC=1 SCAR=6.9
Pod undocumented
32 0 my $c = shift;
33 0 $kill_nicely = 1;
34 0 die();
35 }
36
37 0 ▶sub error {
Subroutine error: not called CC=6 SCAR=37.4
Pod undocumented
38 0 my ($self,$error) = @_;
39 0 local @CARP_NOT = qw(Cake);
40 0 ▶ if ($kill_nicely){
Branch: 1 branch
BranchTrueFalseTotal
if ($kill_nicely) 0 0 0
41 0 $kill_nicely = 0;
42 0 return;
43 }
44
45 0 my ($message,$caller);
46 0 ▶ if (ref $error eq 'HASH'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $error eq "HASH") 0 0 0
47 0 $message = $error->{message};
48 0 $caller = $error->{caller};
49 0 $error = $message.' at '.$caller->[1].' line '.$caller->[2];
50 }
51
52 0 ▶ if ($self->debug){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) { } 0 0 0
53 0 ▶ $error = __PACKAGE__->backtrace($error) if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
54 0 local $SIG{__DIE__} = \&handleErrors($self,$error);
55 } else {
56 0 ▶ if ($self->app->can('errors')){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->app->can('errors')) { } 0 0 0
57 0 $self->app->errors($self,$error);
58 } else {
59 0 $self->status_code('404');
60 0 $self->body('something wrong is going on');
61 }
62 }
63 0 return 1;
64 }
65
66 18 ▶sub log {
Subroutine log: called CC=2 SCAR=7.6
Pod undocumented
67 18 my $self = shift;
68 18 ▶ if (@_ > 1){
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 0 18 18
69 0 push @{$self->app->{log}},\@_;
70 } else {
71 18 push @{$self->app->{log}},shift;
72 }
73 18 return 1;
74 }
75
76 0 ▶sub warn {
Subroutine warn: not called CC=2 SCAR=17.9
Pod undocumented
77 0 my $self = shift;
78 0 my $message = shift;
79 0 my ($caller,$file,$line) = caller;
80 0 ▶ $self->app->{warnings}->{$caller} = [] if !$self->app->{warnings}->{$caller};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->app->{"warnings"}{$caller} 0 0 0
81 0 push @{ $self->app->{warnings}->{$caller} }, {
82 line => $line,
83 message => $message
84 };
85 0 return 1;
86 }
87
88 0 ▶sub handleErrors {
Subroutine handleErrors: not called CC=1 SCAR=6.9
Pod undocumented
89 0 my $self = shift;
90 0 my $message = shift;
91 0 $self->status_code(500);
92 0 $self->body($message);
93 0 return $self;
94 }
95
96 ########proudly stolen from Dancer :P
97 6 ▶sub backtrace {
Subroutine backtrace: called CC=11 SCAR=25.4
Pod undocumented
98 6 my ($self,$message) = @_;
99 6 $message =
100 qq|<pre class="error">| . _html_encode($message) . "</pre>";
101
102 # the default perl warning/error pattern
103 6 my ($file, $line) = ($message =~ /at (\S+) line (\d+)/);
104
105 # the Devel::SimpleTrace pattern
106 6 ▶ ($file, $line) = ($message =~ /at.*\((\S+):(\d+)\)/)
Branch: 1 branch
BranchTrueFalseTotal
unless $file and $line 0 6 6
Condition33%1/3
$file and $line
!ll&&!rl&&r
006
MC/DC0%0/2
$file and $line
$file $line
Truth table33%1/3
A and B
A= $fileB= $line
ABresult
0X0
100
111
107 unless $file and $line;
108
109 # no file/line found, cannot open a file for context
110 6 ▶ return $message unless ($file and $line);
Branch: 1 branch
BranchTrueFalseTotal
unless $file and $line 0 6 6
Condition33%1/3
$file and $line
!ll&&!rl&&r
006
MC/DC0%0/2
$file and $line
$file $line
Truth table33%1/3
A and B
A= $fileB= $line
ABresult
0X0
100
111
111
112 # file and line are located, let's read the source Luke!
113
114 6 ▶ open FILE, "<$file" or return $message;
Branch: 1 branch
BranchTrueFalseTotal
open FILE, "<$file" or return $message 6 0 6
115 6 my @lines = <FILE>;
116 6 close FILE;
117
118 6 my $backtrace = $message;
119
120 6 $backtrace
121 .= qq|<div class="title">| . "$file around line $line" . "</div>";
122
123 6 $backtrace .= qq|<pre class="content">|;
124
125 6 $line--;
126 6 ▶ my $start = (($line - 3) >= 0) ? ($line - 3) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$line - 3 >= 0 ? : 6 0 6
127 6 ▶ my $stop = (($line + 3) < scalar(@lines)) ? ($line + 3) : scalar(@lines);
Branch: 1 branch
BranchTrueFalseTotal
$line + 3 < scalar @lines ? : 6 0 6
128
129 6 ▶ for (my $l = $start; $l <= $stop; $l++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l <= $stop) 42 6 48
130 42 chomp $lines[$l];
131 42 ▶ if ($l == $line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l == $line) { } 6 36 42
132 6 $backtrace
133 .= qq|<span class="nu">|
134 . tabulate($l + 1, $stop + 1)
135 . qq|</span> <span style="color: red;">|
136 . _html_encode($lines[$l])
137 . "</span>\n";
138 }
139 else {
140 36 my $thisline = $lines[$l];
141
142 36 ▶ if ($thisline =~ m/^\s*#/){
Branch: 1 branch
BranchTrueFalseTotal
if ($thisline =~ /^\s*#/) 0 36 36
143 0 $thisline = '<i>'.$thisline.'</i>';
144 }
145
146 $backtrace
147 36 .= qq|<span class="nu">|
148 . tabulate($l + 1, $stop + 1)
149 . "</span> "
150 . _html_encode($thisline) . "\n";
151 }
152 }
153 6 $backtrace .= "</pre>";
154 6 return $backtrace;
155 }
156
157 48 ▶sub _html_encode {
Subroutine _html_encode: called CC=1 SCAR=0.0
158 48 my $value = shift;
159 48 $value =~ s/&/&amp;/g;
160 48 $value =~ s/</&lt;/g;
161 48 $value =~ s/>/&gt;/g;
162 48 $value =~ s/'/&#39;/g;
163 48 $value =~ s/"/&quot;/g;
164 48 return $value;
165 }
166
167 42 ▶sub tabulate {
Subroutine tabulate: called CC=2 SCAR=7.6
Pod undocumented
168 42 my ($number, $max) = @_;
169 42 my $len = length($max);
170 42 ▶ return $number if length($number) == $len;
Branch: 1 branch
BranchTrueFalseTotal
if length $number == $len 42 0 42
171 0 return " $number";
172 }
173
174 8 1;
« Cake/Plugins/Simple/Session.pm ↑ Summary Cake/Utils.pm »