Ex/Die.pm

filter: Statementstmt 37.7% 34 / 90 Branchbran 18.0% 13 / 72 Conditioncond 7.1% 1 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 75.0% 6 / 8 Podpod 0.0% 0 / 1 totaltotal 27.8% 54 / 194 CC 42 SCAR 62.5
CC
42
Cov
27%
die_handler
870.0
import
17.1
BEGIN
4.6
CRAP 519.2
« Ex/Validate.pm ↑ Summary Ex/Dump.pm »
1 package CGI::Ex::Die;
2  
3 =head1 NAME
4  
5 CGI::Ex::Die - A CGI::Carp::FatalsToBrowser type utility.
6  
7 =head1 VERSION
8  
9 version 2.54
10  
11 =cut
12  
13 ###----------------------------------------------------------------###
14 # Copyright - Paul Seamons #
15 # Distributed under the Perl Artistic License without warranty #
16 ###----------------------------------------------------------------###
17  
18 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use vars qw($EXTENDED_ERRORS $SHOW_TRACE $IGNORE_EVAL);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use CGI::Ex;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use CGI::Ex::Dump qw(debug ctrace dex_html);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 our $VERSION = '2.54'; # VERSION
24 2 our $no_recurse;
25 2 our $ERROR_TEMPLATE;
26 2 our $LOG_HANDLER;
27 2 our $FINAL_HANDLER;
28  
29 BEGIN {
30 2 ▶ $SHOW_TRACE = 0 if ! defined $SHOW_TRACE;
Subroutine BEGIN: called CC=4 SCAR=15.2
Branch: 1 branch
BranchTrueFalseTotal
unless defined $CGI::Ex::Die::SHOW_TRACE 2 0 2
31 2 ▶ $IGNORE_EVAL = 0 if ! defined $IGNORE_EVAL;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $CGI::Ex::Die::IGNORE_EVAL 2 0 2
32 2 ▶ $EXTENDED_ERRORS = 1 if ! defined $EXTENDED_ERRORS;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $CGI::Ex::Die::EXTENDED_ERRORS 2 0 2
33 }
34  
35 ###----------------------------------------------------------------###
36  
37 sub import {
38 2 ▶ my $class = shift;
Subroutine import: called CC=11 SCAR=28.4
39 2 ▶ if ($#_ != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ != -1) 1 1 2
40 1 ▶ if (($#_ + 1) % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (($#_ + 1) % 2) 0 1 1
41 0 require Carp;
42 0 &Carp::croak("Usage: use ".__PACKAGE__." register => 1");
43 }
44 1 my %args = @_;
45 ### may be called as
46 # use CGI::Ex::Die register => 1;
47 # OR
48 # use CGI::Ex::Die register => [qw(die)];
49 1 ▶ if (! ref($args{register}) || grep {/die/} @{ $args{register} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $args{"register"} or grep {/die/;} @{$args{"register"};}) 1 0 1
Condition33%1/3
not ref $args{"register"} or grep {/die/;} @{$args{"register"};}
l!l&&r!l&&!r
100
MC/DC0%0/2
not ref $args{"register"} or grep {/die/;} @{$args{"register"};}
not ref $args{"register"} grep {/die/;} @{$args{"register"};}
Truth table33%1/3
not(A) or B
A= not ref $args{"register"}B= grep {/die/;} @{$args{"register"};}
ABresult
1X1
011
000
50 1 $SIG{__DIE__} = \&die_handler;
51 }
52 1 ▶ $SHOW_TRACE = $args{'show_trace'} if exists $args{'show_trace'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"show_trace"} 0 1 1
53 1 ▶ $IGNORE_EVAL = $args{'ignore_eval'} if exists $args{'ignore_eval'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"ignore_eval"} 0 1 1
54 1 ▶ $EXTENDED_ERRORS = $args{'extended_errors'} if exists $args{'extended_errors'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"extended_errors"} 0 1 1
55 1 ▶ $ERROR_TEMPLATE = $args{'error_template'} if exists $args{'error_template'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"error_template"} 0 1 1
56 1 ▶ $LOG_HANDLER = $args{'log_handler'} if exists $args{'log_handler'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"log_handler"} 0 1 1
57 1 ▶ $FINAL_HANDLER = $args{'final_handler'} if exists $args{'final_handler'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"final_handler"} 0 1 1
58 }
59 2 return 1;
60 }
61  
62 ###----------------------------------------------------------------###
63  
64 sub die_handler {
65 0 ▶ my $err = shift;
Subroutine die_handler: not called CC=29 SCAR=67.7
Pod undocumented
66  
67 0 ▶ die $err if $no_recurse;
Branch: 1 branch
BranchTrueFalseTotal
if $CGI::Ex::Die::no_recurse 0 0 0
68 0 local $no_recurse = 1;
69  
70 ### test for eval - if eval - propogate it up
71 0 ▶ if (! $IGNORE_EVAL) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($CGI::Ex::Die::IGNORE_EVAL) 0 0 0
72 0 ▶ if (! $ENV{MOD_PERL}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ENV{'MOD_PERL'}) { } 0 0 0
73 0 my $n = 0;
74 0 ▶ while (my $sub = (caller(++$n))[3]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = (caller ++$n)[3]) 0 0 0
75 0 ▶ next if $sub !~ /eval/;
Branch: 1 branch
BranchTrueFalseTotal
unless $sub =~ /eval/ 0 0 0
76 0 die $err; # die and let the eval catch it
77 }
78  
79 ### test for eval in a mod_perl environment
80 } else {
81 0 my $n = 0;
82 0 my $found = 0;
83 0 ▶ while (my $sub = (caller(++$n))[3]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = (caller ++$n)[3]) 0 0 0
84 0 ▶ $found = $n if ! $found && $sub =~ /eval/;
Branch: 1 branch
BranchTrueFalseTotal
if not $found and $sub =~ /eval/ 0 0 0
Condition0%0/3
not $found and $sub =~ /eval/
!ll&&!rl&&r
000
MC/DC0%0/2
not $found and $sub =~ /eval/
not $found $sub =~ /eval/
Truth table0%0/3
not(A) and B
A= not $foundB= $sub =~ /eval/
ABresult
0X0
100
111
85 0 ▶ last if $sub =~ /^(Apache|ModPerl)::(PerlRun|Registry)/;
Branch: 1 branch
BranchTrueFalseTotal
if $sub =~ /^(Apache|ModPerl)::(PerlRun|Registry)/ 0 0 0
86 }
87 0 ▶ if ($found && $n - 1 != $found) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found and $n - 1 != $found) 0 0 0
Condition0%0/3
$found and $n - 1 != $found
!ll&&!rl&&r
000
MC/DC0%0/2
$found and $n - 1 != $found
$found $n - 1 != $found
Truth table0%0/3
A and B
A= $foundB= $n - 1 != $found
ABresult
0X0
100
111
88 0 die $err;
89 }
90 }
91 }
92  
93 ### decode the message
94 0 ▶ if (ref $err) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $err) { } 0 0 0
elsif ($CGI::Ex::Die::EXTENDED_ERRORS and $err) { } 0 0 0
95  
96 ▶ } elsif ($EXTENDED_ERRORS && $err) {
Condition0%0/3
$CGI::Ex::Die::EXTENDED_ERRORS and $err
!ll&&!rl&&r
000
MC/DC0%0/2
$CGI::Ex::Die::EXTENDED_ERRORS and $err
$CGI::Ex::Die::EXTENDED_ERRORS $err
Truth table0%0/3
A and B
A= $CGI::Ex::Die::EXTENDED_ERRORSB= $err
ABresult
0X0
100
111
97 0 my $copy = "$err";
98 0 ▶ if ($copy =~ m|^Execution of ([/\w\.\-]+) aborted due to compilation errors|si) {
Branch: 2 branches
BranchTrueFalseTotal
if ($copy =~ m[^Execution of ([/\w\.\-]+) aborted due to compilation errors]is) { } 0 0 0
elsif ($copy =~ m[^syntax error at ([/\w.\-]+) line \d+, near]im) { } 0 0 0
99 0 eval {
100 0 ▶ local $SIG{__WARN__} = sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
101 0 require $1;
102 };
103 0 ▶ my $error = $@ || '';
Condition0%0/2
$@ || ''
l!l
00
MC/DC0%0/1
$@ || ''
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
104 0 $error =~ s|Compilation failed in require at [/\w/\.\-]+/Die.pm line \d+\.\s*$||is;
105 0 chomp $error;
106 0 $err .= "\n($error)\n";
107 } elsif ($copy =~ m|^syntax error at ([/\w.\-]+) line \d+, near|mi) {
108 }
109 }
110  
111 ### prepare common args
112 0 my $msg = &CGI::Ex::Dump::_html_quote("$err");
113 0 $msg = "<pre style='background:red;color:white;border:2px solid black;font-size:120%;padding:3px'>Error: $msg</pre>\n";
114 0 ▶ my $ctrace = ! $SHOW_TRACE ? ""
Branch: 1 branch
BranchTrueFalseTotal
!$CGI::Ex::Die::SHOW_TRACE ? : 0 0 0
115 : "<pre style='background:white;color:black;border:2px solid black;padding:3px'>"
116 . dex_html(ctrace)."</pre>";
117 0 my $args = {err => "$err", msg => $msg, ctrace => $ctrace};
118  
119 0 ▶ &$LOG_HANDLER($args) if $LOG_HANDLER;
Branch: 1 branch
BranchTrueFalseTotal
if $CGI::Ex::Die::LOG_HANDLER 0 0 0
120  
121 ### web based - give more options
122 0 ▶ if ($ENV{REQUEST_METHOD}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'REQUEST_METHOD'}) { } 0 0 0
123 0 my $cgix = CGI::Ex->new;
124 0 $| = 1;
125 ### get the template and swap it in
126 # allow for a sub that returns the template
127 # or a string
128 # or a filename (string starting with /)
129 0 my $out;
130 0 ▶ if ($ERROR_TEMPLATE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CGI::Ex::Die::ERROR_TEMPLATE) 0 0 0
131 $out = UNIVERSAL::isa($ERROR_TEMPLATE, 'CODE') ? &$ERROR_TEMPLATE($args) # coderef
132 : (substr($ERROR_TEMPLATE,0,1) ne '/') ? $ERROR_TEMPLATE # html string
133 0 ▶ : do { # filename
Branch: 2 branches
BranchTrueFalseTotal
UNIVERSAL::isa($CGI::Ex::Die::ERROR_TEMPLATE, 'CODE') ? : 0 0 0
substr($CGI::Ex::Die::ERROR_TEMPLATE, 0, 1) ne '/' ? : 0 0 0
134 0 ▶ if (open my $fh, $ERROR_TEMPLATE) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, $CGI::Ex::Die::ERROR_TEMPLATE) 0 0 0
135 0 read($fh, my $str, -s $ERROR_TEMPLATE);
136 0 $str; # return of the do
137 } };
138 }
139 0 ▶ if ($out) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out) { } 0 0 0
140 0 $cgix->swap_template(\$out, $args);
141 } else {
142 0 $out = $msg.'<p></p>'.$ctrace;
143 }
144  
145 ### similar to CGI::Carp
146 0 ▶ if (my $r = $cgix->apache_request) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $cgix->apache_request) { } 0 0 0
147 0 ▶ if ($r->bytes_sent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->bytes_sent) { } 0 0 0
148 0 $r->print($out);
149 } else {
150 0 $r->status(500);
151 0 $r->custom_response(500, $out);
152 }
153 } else {
154 0 $cgix->print_content_type;
155 0 print $out;
156 }
157 } else {
158 ### command line execution
159 }
160  
161 0 ▶ &$FINAL_HANDLER($args) if $FINAL_HANDLER;
Branch: 1 branch
BranchTrueFalseTotal
if $CGI::Ex::Die::FINAL_HANDLER 0 0 0
162  
163 0 die $err;
164 }
165  
166 2 1;
167  
168 __END__
« Ex/Validate.pm ↑ Summary Ex/Dump.pm »