lib/CGI/Carp/Throw.pm

filter: Statementstmt 76.7% 89 / 116 Branchbran 50.0% 21 / 42 Conditioncond 51.7% 15 / 29 MC/DCmcdc 41.1% 7 / 17 Subroutinesub 93.7% 15 / 16 Podpod 50.0% 2 / 4 totaltotal 66.5% 149 / 224 CC 33 SCAR 25.8
CC
33
Cov
67%
die_msg_io
34.7
throw_format_sub
30.0
import
8.5
CRAP 13.3
↑ Summary
1 package CGI::Carp::Throw;
2  
3 #####################################################################
4 # CGI::Carp::Throw
5 #
6 # Provide the ability to represent thrown exceptions as user oriented
7 # messages rather than obvious error messages with technical tracing
8 # information without losing any of the capabilities for providing
9 # error tracing from CGI::Carp.
10 #
11 #####################################################################
12  
13 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 12 ▶use 5.006002;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 12 our $VERSION = '0.04';
19  
20 12 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 # using !/ToBrowser/ on import doesn't work
22 12 ▶use CGI::Carp (
Subroutine BEGIN: called CC=1 SCAR=0.0
23 @CGI::Carp::EXPORT,
24 12 (grep { ! /name=|^wrap$|ToBrowser/ } @CGI::Carp::EXPORT_OK)
25 12 );
26  
27 12 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 12 our @EXPORT = (qw(
30 throw_browser
31 ), @CGI::Carp::EXPORT);
32  
33 12 our @EXPORT_OK = (qw(
34 throw_browser_cloaked throw_format_sub
35 ), @CGI::Carp::EXPORT_OK);
36  
37 our %EXPORT_TAGS = (
38 'all' => [ qw(
39 throw_browser throw_browser_cloaked throw_format_sub
40 12 ), @CGI::Carp::EXPORT, (grep { ! /\^name/ } @CGI::Carp::EXPORT_OK) ],
41 'carp_browser' => [ qw(
42 fatalsToBrowser warningsToBrowser throw_browser
43 ) ]
44 );
45  
46 12 *CGI::Carp::Throw::warningsToBrowser = *CGI::Carp::warningsToBrowser;
47  
48 12 my $final_warn_browser;
49  
50 #####################################################################
51 # Need to call CGI::Carp's import in a controlled manner and with
52 # a controlled environment.
53 #
54 # More complicated than I would like but guessing it's reasonably
55 # robust.
56 #####################################################################
57 12 ▶sub import {
Subroutine import: called CC=8 SCAR=21.4
58 12 my $pkg = shift;
59  
60 # this section mostly taken from CGI::Carp
61 12 my @routines = grep { ! /^(?:name|:)/ } (@_, @EXPORT);
62 12 my($oldlevel) = $Exporter::ExportLevel;
63 12 $Exporter::ExportLevel = 1;
64 12 Exporter::import($pkg,@routines);
65 12 $Exporter::ExportLevel = $oldlevel;
66
67 # already exported CGI:Carp methods but need to make sure
68 # other CGI::Carp import/Exporter functionality sees its arguments
69 my @forward_args = grep
70 12 ▶ { /warningsToBrowser/ or not ($CGI::Carp::Throw::{ $_ } or /^:/) }
Condition83%5/6
/warningsToBrowser/ || !($CGI::Carp::Throw::{$_} || /^:/)
l!l&&r!l&&!r
038
$CGI::Carp::Throw::{$_} || /^:/
l!l&&r!l&&!r
263
MC/DC66%2/3
/warningsToBrowser/ || !($CGI::Carp::Throw::{$_} || /^:/)
/warningsToBrowser/ $CGI::Carp::Throw::{$_} /^:/
Truth table75%3/4
A || not(B || C)
A= /warningsToBrowser/B= $CGI::Carp::Throw::{$_}C= /^:/
ABCresult
1XX1
0001
01X0
0010
71 @_;
72  
73 12 ▶ if (grep { /:(?:DEFAULT|carp_browser)/i } @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/:(?:DEFAULT|carp_browser)/i;} @_) 6 6 12
74 6 $final_warn_browser = 1;
75 6 foreach my $to_brow (qw(fatalsToBrowser warningsToBrowser)) {
76 push @forward_args, $to_brow
77 12 ▶ unless (grep { /^$to_brow$/ } @forward_args);
Branch: 1 branch
BranchTrueFalseTotal
unless grep {/^$to_brow$/;} @forward_args 12 0 12
78 }
79 }
80
81 # compatibility with old CGI::Carp
82 12 ▶ if ($CGI::Carp::VERSION =~ /(\d*\.?\d*)/ and $1 < 1.24) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CGI::Carp::VERSION =~ /(\d*\.?\d*)/ and $1 < "1.24") 0 12 12
Condition33%1/3
$CGI::Carp::VERSION =~ /(\d*\.?\d*)/ and $1 < "1.24"
!ll&&!rl&&r
0120
MC/DC0%0/2
$CGI::Carp::VERSION =~ /(\d*\.?\d*)/ and $1 < "1.24"
$CGI::Carp::VERSION =~ /(\d*\.?\d*)/ $1 < "1.24"
Truth table33%1/3
A and B
A= $CGI::Carp::VERSION =~ /(\d*\.?\d*)/B= $1 < "1.24"
ABresult
0X0
100
111
83 0 @forward_args = grep { ! /^name=/ } @forward_args
84 }
85  
86 # be a bit careful what we might (re?)import to Throw module
87 12 local @CGI::Carp::EXPORT = ();
88 12 CGI::Carp::import($pkg, @forward_args);
89 }
90  
91 my $throw_cloaked;
92  
93 #####################################################################
94 # Do a little bit of message formatting where important.
95 # Basically get rid of some lines of confess information that reflect
96 # internal machinery and might be confusing and add a package marker.
97 #
98 # Add <html> <head> and <body> tags if they appear to be missing.
99 #####################################################################
100 4 ▶sub massage_mess {
Subroutine massage_mess: called CC=5 SCAR=16.2
Pod undocumented
101 4 my $mess = shift;
102  
103 4 ▶ unless ($throw_cloaked) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($throw_cloaked) 3 1 4
104 3 my $confess_mess = CGI::Carp::_longmess;
105 3 $confess_mess =~ s/.*CGI::Carp(?!::Throw::)(?:.*?)line\s+\d*\s*//s;
106 3 $confess_mess =~ s/\s*CGI::Carp::Throw::_throw(?:.*?)line\s+\d*\s*?\n//;
107 # make package a variable
108 3 $mess .= '<!-- ' . __PACKAGE__ . " tracing\n$confess_mess-->";
109 }
110
111 4 ▶ unless ($mess =~ /<\s*html\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mess =~ /<\s*html\b/i) 4 0 4
112 4 ▶ unless ($mess =~ /<\s*body\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mess =~ /<\s*body\b/i) 4 0 4
113 4 $mess = "\n<body>\n$mess\n</body>\n";
114 }
115 4 ▶ unless ($mess =~ /<\s*head\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mess =~ /<\s*head\b/i) 4 0 4
116 4 $mess = "\n<head><title>CGI::Carp::Throw page.</title></head>\n$mess";
117 }
118 4 $mess = "<html>\n$mess\n</html>\n";
119 }
120  
121 4 return $mess;
122 }
123  
124  
125 #####################################################################
126 # Lifted in large part from CGI::Carp
127 #####################################################################
128 4 ▶sub die_msg_io {
Subroutine die_msg_io: called CC=9 SCAR=35.5
Pod undocumented
129 4 my $mess = massage_mess(shift);
130  
131 4 my $mod_perl = exists $ENV{MOD_PERL};
132 4 ▶ if ($mod_perl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod_perl) { } 0 4 4
133 0 my $r;
134 0 ▶ if ($ENV{MOD_PERL_API_VERSION} && $ENV{MOD_PERL_API_VERSION} == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'MOD_PERL_API_VERSION'} and $ENV{'MOD_PERL_API_VERSION'} == 2) { } 0 0 0
Condition0%0/3
$ENV{"MOD_PERL_API_VERSION"} and $ENV{"MOD_PERL_API_VERSION"} == 2
!ll&&!rl&&r
000
MC/DC0%0/2
$ENV{"MOD_PERL_API_VERSION"} and $ENV{"MOD_PERL_API_VERSION"} == 2
$ENV{"MOD_PERL_API_VERSION"} $ENV{"MOD_PERL_API_VERSION"} == 2
Truth table0%0/3
A and B
A= $ENV{"MOD_PERL_API_VERSION"}B= $ENV{"MOD_PERL_API_VERSION"} == 2
ABresult
0X0
100
111
135 0 $mod_perl = 2;
136 0 require Apache2::RequestRec;
137 0 require Apache2::RequestIO;
138 0 require Apache2::RequestUtil;
139 0 require APR::Pool;
140 0 require ModPerl::Util;
141 0 require Apache2::Response;
142 0 $r = Apache2::RequestUtil->request;
143 }
144 else {
145 0 $r = Apache->request;
146 }
147 # If bytes have already been sent, then
148 # we print the message out directly.
149 # Otherwise we make a custom error
150 # handler to produce the doc for us.
151 0 ▶ if ($r->bytes_sent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->bytes_sent) { } 0 0 0
152 0 $r->print($mess);
153 0 ▶ $mod_perl == 2 ? ModPerl::Util::exit(0) : $r->exit;
Branch: 1 branch
BranchTrueFalseTotal
$mod_perl == 2 ? : 0 0 0
154 } else {
155 # MSIE won't display a custom 500 response unless it is >512 bytes!
156 0 ▶ if ($ENV{HTTP_USER_AGENT} =~ /MSIE/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"HTTP_USER_AGENT"} =~ /MSIE/) 0 0 0
157 0 $mess = "<!-- " . (' ' x 513) . " -->\n$mess";
158 }
159 0 $r->custom_response(500,$mess);
160 }
161 } else {
162 4 my $bytes_written = eval{tell STDOUT};
163 4 ▶ if (defined $bytes_written && $bytes_written > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bytes_written and $bytes_written > 0) { } 3 1 4
Condition66%2/3
defined $bytes_written and $bytes_written > 0
!ll&&!rl&&r
013
MC/DC50%1/2
defined $bytes_written and $bytes_written > 0
defined $bytes_written $bytes_written > 0
Truth table66%2/3
A and B
A= defined $bytes_writtenB= $bytes_written > 0
ABresult
0X0
100
111
164 3 print STDOUT $mess;
165 }
166 else {
167 1 print STDOUT "Content-type: text/html\n\n";
168 1 print STDOUT $mess;
169 }
170 }
171 }
172  
173 12 my $throw_format_fref;
174  
175 #####################################################################
176 # Set / retrieve the throw_format_sub class attribute
177 #
178 # throw_format_sub class attribute is a user supplied routine to
179 # format error messages in some format, probably using template
180 # technology, resulting in an appearance compatible with a web site.
181 #####################################################################
182 0 ▶sub throw_format_sub {
Subroutine throw_format_sub: not called CC=5 SCAR=34.0
Pod documented
183
184 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
185 0 my $new_fref = shift;
186
187 0 ▶ croak 'throw_format_sub setting must be code reference'
Branch: 1 branch
BranchTrueFalseTotal
if $new_fref and not ref $new_fref && ref($new_fref) =~ /CODE/i 0 0 0
Condition0%0/6
$new_fref and not ref $new_fref && ref($new_fref) =~ /CODE/i
!ll&&!rl&&r
000
ref $new_fref && ref($new_fref) =~ /CODE/i
!ll&&!rl&&r
000
MC/DC0%0/3
$new_fref and not ref $new_fref && ref($new_fref) =~ /CODE/i
$new_fref ref $new_fref ref($new_fref) =~ /CODE/i
Truth table0%0/4
A and not(B && C)
A= $new_frefB= ref $new_frefC= ref($new_fref) =~ /CODE/i
ABCresult
0XX0
1110
10X1
1101
188 if ( $new_fref and
189 ( (not ref($new_fref)) or
190 ref($new_fref) !~ /CODE/i
191 )
192 );
193
194 0 $throw_format_fref = $new_fref;
195 }
196
197 0 return $throw_format_fref;
198 }
199  
200 12 my $old_fatals_to_browser = \&CGI::Carp::fatalsToBrowser;
201  
202 {
203 12 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
204  
205 #####################################################################
206 # Partially replace fatalsToBrowser so that it gets called
207 # unless the exception came from one of our throw_browser routines.
208 #####################################################################
209 11 ▶*CGI::Carp::fatalsToBrowser = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
210 11 my $msg = shift;
211
212 11 my($pack,undef,undef,$sub) = caller(2);
213 11 ▶ if (($sub || '') =~ /::_throw_browser$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (($sub || '') =~ /::_throw_browser$/) { } 4 7 11
Condition100%2/2
$sub || ''
l!l
92
MC/DC100%1/1
$sub || ''
$sub
Truth table100%2/2
A
A= $sub
Aresult
11
00
214 4 die_msg_io($msg);
215 }
216 else {
217 7 $old_fatals_to_browser->($msg)
218 }
219 12 };
220 }
221  
222 #####################################################################
223 # Shared throw browser logic for cloaked and non-cloaked variants.
224 #
225 # If you called this you wanted CGI::Carp wrapping (unless you're in
226 # an eval) so turn that on. If a formatting routine was specified
227 # call it and die with its message. Otherwise die and let the
228 # fatalsToBrowser replacement take over.
229 #####################################################################
230 6 ▶sub _throw_browser {
Subroutine _throw_browser: called CC=7 SCAR=21.3
231 6 ▶ unless ($CGI::Carp::WRAP or CGI::Carp::ineval) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($CGI::Carp::WRAP or CGI::Carp::ineval()) 2 4 6
Condition100%3/3
$CGI::Carp::WRAP or CGI::Carp::ineval()
l!l&&r!l&&!r
312
MC/DC100%2/2
$CGI::Carp::WRAP or CGI::Carp::ineval()
$CGI::Carp::WRAP CGI::Carp::ineval()
Truth table100%3/3
A or B
A= $CGI::Carp::WRAPB= CGI::Carp::ineval()
ABresult
1X1
011
000
232 2 $CGI::Carp::WRAP++;
233 }
234
235 6 ▶ if ($throw_format_fref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($throw_format_fref) { } 0 6 6
236 0 my $die_msg = $throw_format_fref->(@_);
237 0 ▶ $die_msg =~ s/([^\n])$/$1\n/ if $die_msg;
Branch: 1 branch
BranchTrueFalseTotal
if $die_msg 0 0 0
238 0 die $die_msg;
239 }
240 else {
241 6 ▶ if ($_[-1] and $_[-1] !~ /\n$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[-1] and not $_[-1] =~ /\n$/) { } 4 2 6
Condition66%2/3
$_[-1] and not $_[-1] =~ /\n$/
!ll&&!rl&&r
024
MC/DC50%1/2
$_[-1] and not $_[-1] =~ /\n$/
$_[-1] not $_[-1] =~ /\n$/
Truth table66%2/3
A and not(B)
A= $_[-1]B= not $_[-1] =~ /\n$/
ABresult
0X0
100
111
242 4 die @_, "\n";
243 }
244 else {
245 2 die @_;
246 }
247 }
248 }
249  
250 #####################################################################
251 # Standard throw browser. "Uncloaked" which includes stack trace
252 # HTML comment.
253 #####################################################################
254 5 ▶sub throw_browser {
Subroutine throw_browser: called CC=1 SCAR=0.0
255 5 undef $throw_cloaked;
256 5 _throw_browser(@_);
257 }
258  
259 #####################################################################
260 # Standard throw browser. "Cloaked" to hide stack trace HTML comment.
261 #####################################################################
262 1 ▶sub throw_browser_cloaked {
Subroutine throw_browser_cloaked: called CC=1 SCAR=0.0
Pod documented
263 1 $throw_cloaked = 1;
264 1 _throw_browser(@_);
265 }
266  
267 12 ▶END {
Subroutine END: called CC=2 SCAR=6.9
268 12 ▶ CGI::Carp::warningsToBrowser(1) if $final_warn_browser;
Branch: 1 branch
BranchTrueFalseTotal
if $final_warn_browser 6 6 12
269 }
270  
271 12 1;
272 __END__
↑ Summary