| 1 | package CGI::ExceptionManager::StackTrace; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | # from MENTA and NanoA | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | sub _escape_html { | ||||||||||||||||||||||||||||||||||||||||||
| 8 | 80 | ▶ | my $str = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_escape_html: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 80 | $str =~ s/&/&/g; | |||||||||||||||||||||||||||||||||||||||||
| 10 | 80 | $str =~ s/>/>/g; | |||||||||||||||||||||||||||||||||||||||||
| 11 | 80 | $str =~ s/</</g; | |||||||||||||||||||||||||||||||||||||||||
| 12 | 80 | $str =~ s/"/"/g; | |||||||||||||||||||||||||||||||||||||||||
| 13 | 80 | $str =~ s/'/'/g; | |||||||||||||||||||||||||||||||||||||||||
| 14 | 80 | return $str; | |||||||||||||||||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 18 | 4 | ▶ | my ($klass, $message) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=19.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 4 | my @trace; | |||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 4 | ▶ | for (my $i = 1; my ($package, $file, $line) = caller($i); $i++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 17 | push @trace, { | |||||||||||||||||||||||||||||||||||||||||
| 23 | file => $file, | ||||||||||||||||||||||||||||||||||||||||||
| 24 | line => $line, | ||||||||||||||||||||||||||||||||||||||||||
| 25 | func => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 26 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 17 | ▶ | if (my @c = caller($i + 1)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 13 | ▶ | $trace[-1]->{func} = $c[3] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | if $c[3]; | ||||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 4 | ▶ | if ($message =~ / at ([^ ]+) line (\d+)/ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$message =~ / at ([^ ]+) line (\d+)/ and $1 ne $trace[0]{"file"} || $2 != $trace[0]{"line"}
$1 ne $trace[0]{"file"} or $2 != $trace[0]{"line"}
MC/DC33%1/3
$message =~ / at ([^ ]+) line (\d+)/ and $1 ne $trace[0]{"file"} || $2 != $trace[0]{"line"}
$message =~ / at ([^ ]+) line (\d+)/
$1 ne $trace[0]{"file"}
$2 != $trace[0]{"line"}
Truth table50%2/4
A and B or C
A= $message =~ / at ([^ ]+) line (\d+)/B= $1 ne $trace[0]{"file"}C= $2 != $trace[0]{"line"}
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | && ($1 ne $trace[0]->{file} || $2 != $trace[0]->{line})) { | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 1 | unshift @trace, { | |||||||||||||||||||||||||||||||||||||||||
| 35 | file => $1, | ||||||||||||||||||||||||||||||||||||||||||
| 36 | line => $2, | ||||||||||||||||||||||||||||||||||||||||||
| 37 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | bless { | ||||||||||||||||||||||||||||||||||||||||||
| 41 | 4 | message => $message, | |||||||||||||||||||||||||||||||||||||||||
| 42 | trace => \@trace, | ||||||||||||||||||||||||||||||||||||||||||
| 43 | }, $klass; | ||||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | sub _build_context { | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 9 | ▶ | my ($file, $linenum) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_context: called CC=8 SCAR=20.9
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 9 | my $code; | |||||||||||||||||||||||||||||||||||||||||
| 49 | 9 | ▶ | if (-f $file) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 9 | my $start = $linenum - 3; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 9 | my $end = $linenum + 3; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 9 | ▶ | $start = $start < 1 ? 1 : $start; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 2 | ▶ | open my $fh, '<:encoding(utf8)', $file | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | or die "cannot open $file:$!"; | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 9 | my $cur_line = 0; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 9 | ▶ | while (my $line = <$fh>) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 232 | ++$cur_line; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 232 | ▶ | last if $cur_line > $end; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 223 | ▶ | next if $cur_line < $start; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 63 | $line =~ s|\t| |g; | |||||||||||||||||||||||||||||||||||||||||
| 61 | 63 | ▶ | my @tag = $cur_line == $linenum | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | ? (q{<b style="color: #000;background-color: #f99;">}, '</b>') | ||||||||||||||||||||||||||||||||||||||||||
| 63 | : ('', ''); | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 63 | $code .= sprintf( | |||||||||||||||||||||||||||||||||||||||||
| 65 | '%s%5d: %s%s', $tag[0], $cur_line, _escape_html($line), | ||||||||||||||||||||||||||||||||||||||||||
| 66 | $tag[1], | ||||||||||||||||||||||||||||||||||||||||||
| 67 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 9 | close $file; | |||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 9 | return $code; | |||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | sub as_html { | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 2 | ▶ | my ($err, %args) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_html: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 2 | my $msg = _escape_html($err->{message}); | |||||||||||||||||||||||||||||||||||||||||
| 77 | 2 | my $out = qq{<!doctype html><head><title>500 Internal Server Error</title><style type="text/css">body { margin: 0; padding: 0; background: rgb(230, 230, 230); color: rgb(44, 44, 44); } h1 { margin: 0 0 .5em; padding: .25em .5em .1em 1.5em; border-bottom: thick solid rgb(0, 0, 15); background: rgb(63, 63, 63); color: rgb(239, 239, 239); font-size: x-large; } p { margin: .5em 1em; } li { font-size: small; } pre { background: rgb(255, 239, 239); color: rgb(47, 47, 47); font-size: medium; } pre code strong { color: rgb(0, 0, 0); background: rgb(255, 143, 143); } p.f { text-align: right; font-size: xx-small; } p.f span { font-size: medium; }</style></head><h1>500 Internal Server Error</h1><p>${msg}</p><ol>}; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 2 | for my $stack (@{$err->{trace}}) { | |||||||||||||||||||||||||||||||||||||||||
| 79 | $out .= join( | ||||||||||||||||||||||||||||||||||||||||||
| 80 | '', | ||||||||||||||||||||||||||||||||||||||||||
| 81 | '<li>', | ||||||||||||||||||||||||||||||||||||||||||
| 82 | $stack->{func} ? _escape_html("in $stack->{func}") : '', | ||||||||||||||||||||||||||||||||||||||||||
| 83 | ' at ', | ||||||||||||||||||||||||||||||||||||||||||
| 84 | $stack->{file} ? _escape_html($stack->{file}) : '', | ||||||||||||||||||||||||||||||||||||||||||
| 85 | ' line ', | ||||||||||||||||||||||||||||||||||||||||||
| 86 | $stack->{line}, | ||||||||||||||||||||||||||||||||||||||||||
| 87 | q(<pre><code>), | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 9 | ▶ | _build_context($stack->{file}, $stack->{line}) || '', | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition50%1/2
_build_context($stack->{'file'}, $stack->{'line'}) || ''
MC/DC0%0/1
_build_context($stack->{'file'}, $stack->{'line'}) || ''
_build_context($stack->{'file'}, $stack->{'line'})
Truth table50%1/2
A
A= _build_context($stack->{'file'}, $stack->{'line'})
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | q(</code></pre></li>), | ||||||||||||||||||||||||||||||||||||||||||
| 90 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 2 | $out .= qq{</ol><p class="f"><span>Powered by $args{powered_by}</p>}; | |||||||||||||||||||||||||||||||||||||||||
| 93 | 2 | $out; | |||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | sub output { | ||||||||||||||||||||||||||||||||||||||||||
| 97 | 3 | ▶ | my ($err, %args) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
output: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 3 | warn $err->{message}; | |||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 3 | print "Status: 500\r\n"; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 3 | print "Content-type: text/html; charset=utf-8\r\n"; | |||||||||||||||||||||||||||||||||||||||||
| 103 | 3 | print "\r\n"; | |||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 3 | ▶ | my $body = $args{renderer} ? $args{renderer}->($err, %args) : $err->as_html(%args); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 3 | utf8::encode($body); | |||||||||||||||||||||||||||||||||||||||||
| 107 | 3 | print $body; | |||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||