| 1 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 2 | # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de> | ||||||||||||||||||||||||||||
| 3 | # File: GX/Exception/Formatter/HTML.pm | ||||||||||||||||||||||||||||
| 4 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package GX::Exception::Formatter::HTML; | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use GX::HTML::Util qw( escape_html ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 14 | # Class setup | ||||||||||||||||||||||||||||
| 15 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 3 | ▶ | use GX::Class::Object; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 3 | build; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 23 | # Public methods | ||||||||||||||||||||||||||||
| 24 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | sub format { | ||||||||||||||||||||||||||
|
Subroutine
format: not called CC=20 SCAR=60.4
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | ▶ | my $self = ref $_[0] ? shift : shift->new; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 0 | my $exception = shift; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | return unless blessed $exception && $exception->isa( 'GX::Exception' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
builtin::blessed($exception) and $exception->isa("GX::Exception")
MC/DC0%0/2
builtin::blessed($exception) and $exception->isa("GX::Exception")
builtin::blessed($exception)
$exception->isa("GX::Exception")
Truth table0%0/3
A and B
A= builtin::blessed($exception)B= $exception->isa("GX::Exception")
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 0 | my @html; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | push @html, '<div class="exception">'; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 0 | push @html, '<h1>' . ref( $exception ) . '</h1>'; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | push @html, | |||||||||||||||||||||||||||
| 40 | '<div class="message">', | ||||||||||||||||||||||||||||
| 41 | '<code>' . join( '<br />', split( /\n/, escape_html( $exception->as_string( 0 ) ) ) ) . '</code>', | ||||||||||||||||||||||||||||
| 42 | '</div>'; | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | my @subexceptions; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | for ( my $subexception = $exception->subexception; $subexception; $subexception = $subexception->subexception ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | push @subexceptions, $subexception; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | if ( @subexceptions ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | push @html, | |||||||||||||||||||||||||||
| 53 | '<div class="subexceptions">', | ||||||||||||||||||||||||||||
| 54 | '<h2>Subexceptions</h2>'; | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | for my $subexception ( @subexceptions ) { | |||||||||||||||||||||||||||
| 57 | 0 | push @html, | |||||||||||||||||||||||||||
| 58 | '<div class="message">', | ||||||||||||||||||||||||||||
| 59 | '<code>' . join( '<br />', split( /\n/, escape_html( $subexception->as_string( 0 ) ) ) ) . '</code>', | ||||||||||||||||||||||||||||
| 60 | '</div>'; | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | push @html, '</div>'; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | if ( $exception->stack_trace ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | push @html, | |||||||||||||||||||||||||||
| 70 | '<div class="stack_trace">', | ||||||||||||||||||||||||||||
| 71 | '<h2>Stack trace</h2>', | ||||||||||||||||||||||||||||
| 72 | '<ol>'; | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | for my $frame ( $exception->stack_trace ) { | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | my $subroutine = $frame->subroutine or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | my $filename = $frame->filename or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | my $line = $frame->line or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | push @html, '<li>'; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | if ( $subroutine eq '(eval)' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | push @html, sprintf( | |||||||||||||||||||||||||||
| 84 | "<p><strong>(eval)</strong> in <strong>%s</strong> at line <strong>%s</strong></p>", | ||||||||||||||||||||||||||||
| 85 | escape_html( $filename ), | ||||||||||||||||||||||||||||
| 86 | escape_html( $line ) | ||||||||||||||||||||||||||||
| 87 | ); | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | else { | ||||||||||||||||||||||||||||
| 90 | 0 | push @html, sprintf( | |||||||||||||||||||||||||||
| 91 | "<p><strong>%s</strong> called in <strong>%s</strong> at line <strong>%s</strong></p>", | ||||||||||||||||||||||||||||
| 92 | escape_html( $subroutine ), | ||||||||||||||||||||||||||||
| 93 | escape_html( $filename ), | ||||||||||||||||||||||||||||
| 94 | escape_html( $line ) | ||||||||||||||||||||||||||||
| 95 | ); | ||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # We don't know the encoding of the source file, so we'll have to guess ... | ||||||||||||||||||||||||||||
| 99 | 0 | ▶ | if ( -f $filename && open( my $fh, '<:encoding(utf8)', $filename ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-f $filename and open my $fh, "<:encoding(utf8)", $filename
MC/DC0%0/2
-f $filename and open my $fh, "<:encoding(utf8)", $filename
-f $filename
open my $fh, "<:encoding(utf8)", $filename
Truth table0%0/3
A and B
A= -f $filenameB= open my $fh, "<:encoding(utf8)", $filename
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | my $first_line = $line - 5; | |||||||||||||||||||||||||||
| 102 | 0 | my $last_line = $line + 5; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | $first_line = 1 if $first_line < 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | push @html, | |||||||||||||||||||||||||||
| 107 | '<div class="viewport">', | ||||||||||||||||||||||||||||
| 108 | '<table class="source" summary="' . escape_html( $filename ) . '">', | ||||||||||||||||||||||||||||
| 109 | '<tbody>'; | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | my $current_line = 0; | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | ▶ | while ( <$fh> ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | $current_line++; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | next if $current_line < $first_line; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | last if $current_line > $last_line; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | my $source = $_; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | chomp( $source ); | |||||||||||||||||||||||||||
| 123 | 0 | $source = escape_html( $source ); | |||||||||||||||||||||||||||
| 124 | 0 | $source =~ s/\s/ /g; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | ▶ | push @html, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | '<tr' . ( $current_line == $line ? ' class="highlight"' : '' ) . '>', | ||||||||||||||||||||||||||||
| 128 | '<td class="line"><code>' . $current_line . '</code></td>', | ||||||||||||||||||||||||||||
| 129 | '<td class="code"><code>' . $source . '</code></td>', | ||||||||||||||||||||||||||||
| 130 | '</tr>'; | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | close $fh; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | push @html, | |||||||||||||||||||||||||||
| 137 | '</tbody>', | ||||||||||||||||||||||||||||
| 138 | '</table>', | ||||||||||||||||||||||||||||
| 139 | '</div>'; | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | push @html, '</li>'; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | push @html, | |||||||||||||||||||||||||||
| 148 | '</ol>', | ||||||||||||||||||||||||||||
| 149 | '</div>'; | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | push @html, '</div>'; | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 0 | return join( "\n", @html ) . "\n"; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 3 | 1; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | __END__ | ||||||||||||||||||||||||||||