| 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
| |||||||||||||||||||||||||||||
| 31 | 2 | ▶ | $IGNORE_EVAL = 0 if ! defined $IGNORE_EVAL; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 2 | ▶ | $EXTENDED_ERRORS = 1 if ! defined $EXTENDED_ERRORS; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 40 | 1 | ▶ | if (($#_ + 1) % 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
not ref $args{"register"} or grep {/die/;} @{$args{"register"};}
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"};}
| |||||||||||||||||||||||||||||
| 50 | 1 | $SIG{__DIE__} = \&die_handler; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | 1 | ▶ | $SHOW_TRACE = $args{'show_trace'} if exists $args{'show_trace'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 1 | ▶ | $IGNORE_EVAL = $args{'ignore_eval'} if exists $args{'ignore_eval'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 1 | ▶ | $EXTENDED_ERRORS = $args{'extended_errors'} if exists $args{'extended_errors'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 1 | ▶ | $ERROR_TEMPLATE = $args{'error_template'} if exists $args{'error_template'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 1 | ▶ | $LOG_HANDLER = $args{'log_handler'} if exists $args{'log_handler'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 1 | ▶ | $FINAL_HANDLER = $args{'final_handler'} if exists $args{'final_handler'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 68 | 0 | local $no_recurse = 1; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | ### test for eval - if eval - propogate it up | ||||||||||||||||||||||||||||
| 71 | 0 | ▶ | if (! $IGNORE_EVAL) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | if (! $ENV{MOD_PERL}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | my $n = 0; | |||||||||||||||||||||||||||
| 74 | 0 | ▶ | while (my $sub = (caller(++$n))[3]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | next if $sub !~ /eval/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | $found = $n if ! $found && $sub =~ /eval/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $found and $sub =~ /eval/
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/
| |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | last if $sub =~ /^(Apache|ModPerl)::(PerlRun|Registry)/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | 0 | ▶ | if ($found && $n - 1 != $found) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$found and $n - 1 != $found
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
| |||||||||||||||||||||||||||||
| 88 | 0 | die $err; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | ### decode the message | ||||||||||||||||||||||||||||
| 94 | 0 | ▶ | if (ref $err) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | ▶ | } elsif ($EXTENDED_ERRORS && $err) { | |||||||||||||||||||||||||||
|
Condition0%0/3
$CGI::Ex::Die::EXTENDED_ERRORS and $err
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
| |||||||||||||||||||||||||||||
| 97 | 0 | my $copy = "$err"; | |||||||||||||||||||||||||||
| 98 | 0 | ▶ | if ($copy =~ m|^Execution of ([/\w\.\-]+) aborted due to compilation errors|si) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
$@ || ''
MC/DC0%0/1
$@ || ''
$@
Truth table0%0/2
A
A= $@
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | ### web based - give more options | ||||||||||||||||||||||||||||
| 122 | 0 | ▶ | if ($ENV{REQUEST_METHOD}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | if (open my $fh, $ERROR_TEMPLATE) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 147 | 0 | ▶ | if ($r->bytes_sent) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 0 | die $err; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 2 | 1; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | __END__ | ||||||||||||||||||||||||||||