lib/HTML/Mason/Exceptions.pm

filter: Statementstmt 94.7% 145 / 153 Branchbran 90.0% 45 / 50 Conditioncond 86.9% 20 / 23 MC/DCmcdc 66.6% 8 / 12 Subroutinesub 95.8% 23 / 24 Podpod 33.3% 5 / 15 totaltotal 88.8% 246 / 277 SCAR 36.8
CC
39
Cov
93%
get_file_context
10.0
filtered_frames
8.0
analyze_error
7.0
CRAP 39.5
« lib/HTML/Mason/Component.pm ↑ Summary script/mason.pl »
1 package HTML::Mason::Exceptions;
2 34 $HTML::Mason::Exceptions::VERSION = '1.58';
3 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 34 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 34 my %e;
7  
8 BEGIN
9 {
10 34 ▶ %e = ( 'HTML::Mason::Exception' =>
Subroutine BEGIN: called CC=1 SCAR=0.0
11 { description => 'generic base class for all Mason exceptions',
12 alias => 'error'},
13  
14 'HTML::Mason::Exception::Abort' =>
15 { isa => 'HTML::Mason::Exception',
16 fields => [qw(aborted_value)],
17 description => 'a component called $m->abort' },
18  
19 'HTML::Mason::Exception::Decline' =>
20 { isa => 'HTML::Mason::Exception',
21 fields => [qw(declined_value)],
22 description => 'a component called $m->decline' },
23  
24 'HTML::Mason::Exception::Compiler' =>
25 { isa => 'HTML::Mason::Exception',
26 alias => 'compiler_error',
27 description => 'error thrown from the compiler' },
28  
29 'HTML::Mason::Exception::Compilation' =>
30 { isa => 'HTML::Mason::Exception',
31 alias => 'compilation_error',
32 fields => [qw(filename)],
33 description => "error thrown in eval of the code for a component" },
34  
35 'HTML::Mason::Exception::Compilation::IncompatibleCompiler' =>
36 { isa => 'HTML::Mason::Exception::Compilation',
37 alias => 'wrong_compiler_error',
38 description => "a component was compiled by a compiler/lexer with incompatible options. recompilation is needed" },
39  
40 'HTML::Mason::Exception::Params' =>
41 { isa => 'HTML::Mason::Exception',
42 alias => 'param_error',
43 description => 'invalid parameters were given to a method/function' },
44  
45 'HTML::Mason::Exception::Syntax' =>
46 { isa => 'HTML::Mason::Exception',
47 alias => 'syntax_error',
48 fields => [qw(source_line comp_name line_number)],
49 description => 'invalid syntax was found in a component' },
50  
51 'HTML::Mason::Exception::System' =>
52 { isa => 'HTML::Mason::Exception',
53 alias => 'system_error',
54 description => 'a system call of some sort failed' },
55  
56 'HTML::Mason::Exception::TopLevelNotFound' =>
57 { isa => 'HTML::Mason::Exception',
58 alias => 'top_level_not_found_error',
59 description => 'the top level component could not be found' },
60  
61 'HTML::Mason::Exception::VirtualMethod' =>
62 { isa => 'HTML::Mason::Exception',
63 alias => 'virtual_error',
64 description => 'a virtual method was not overridden' },
65  
66 );
67 }
68  
69 34 ▶use Exception::Class (%e);
Subroutine BEGIN: called CC=1 SCAR=0.0
70  
71 34 HTML::Mason::Exception->Trace(1);
72  
73 # To avoid circular reference between exception and request.
74 34 HTML::Mason::Exception->NoRefs(1);
75  
76 # The import() method allows this:
77 # use HTML::Mason::Exceptions(abbr => ['error1', 'error2', ...]);
78 # ...
79 # error1 "something went wrong";
80  
81 sub import
82 {
83 387 ▶ my ($class, %args) = @_;
Subroutine import: called CC=4 SCAR=13.9
84  
85 387 my $caller = caller;
86 387 ▶ if ($args{abbr})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"abbr"}) 383 4 387
87 {
88 383 foreach my $name (@{$args{abbr}})
89 {
90 34 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
91 928 ▶ die "Unknown exception abbreviation '$name'" unless defined &{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined &{$name;} 0 928 928
92 928 *{"${caller}::$name"} = \&{$name};
93 }
94 }
95 {
96 34 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
97 387 *{"${caller}::isa_mason_exception"} = \&isa_mason_exception;
98 387 *{"${caller}::rethrow_exception"} = \&rethrow_exception;
99 }
100 }
101  
102 sub isa_mason_exception
103 {
104 1432 ▶ my ($err, $name) = @_;
Subroutine isa_mason_exception: called CC=4 SCAR=13.9
Pod undocumented
105 1432 ▶ return unless defined $err;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $err 164 1268 1432
106  
107 1268 ▶ $name = $name ? "HTML::Mason::Exception::$name" : "HTML::Mason::Exception";
Branch: 1 branch
BranchTrueFalseTotal
$name ? : 1104 164 1268
108 34 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
109 1268 ▶ die "no such exception class $name" unless $name->isa('HTML::Mason::Exception');
Branch: 1 branch
BranchTrueFalseTotal
unless $name->isa("HTML::Mason::Exception") 0 1268 1268
110  
111 1268 return UNIVERSAL::isa($err, $name);
112 }
113  
114 sub rethrow_exception
115 {
116 2590 ▶ my ($err) = @_;
Subroutine rethrow_exception: called CC=4 SCAR=13.9
Pod undocumented
117 2590 ▶ return unless $err;
Branch: 1 branch
BranchTrueFalseTotal
unless $err 1656 934 2590
118  
119 934 ▶ if ( UNIVERSAL::can($err, 'rethrow') ) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::can($err, 'rethrow')) { } 869 65 934
elsif (ref $err) { } 1 64 65
120 869 $err->rethrow;
121 }
122 elsif ( ref $err ) {
123 1 die $err;
124 }
125 64 HTML::Mason::Exception->throw(error => $err);
126 }
127  
128 package HTML::Mason::Exception;
129 34 $HTML::Mason::Exception::VERSION = '1.58';
130 use HTML::Mason::MethodMaker
131 34 ▶ ( read_write => [ qw ( format ) ] );
Subroutine BEGIN: called CC=1 SCAR=0.0
132  
133 sub new
134 {
135 164 ▶ my ($class, %params) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
136  
137 164 my $self = $class->SUPER::new(%params);
138 164 $self->format('text');
139 164 return $self;
140 }
141  
142 # If we create a new exception from a Mason exception, just use the
143 # short error message, not the stringified exception. Otherwise
144 # exceptions can get stringified more than once.
145 sub throw
146 {
147 164 ▶ my $class = shift;
Subroutine throw: called CC=4 SCAR=14.0
Pod documented
148 164 ▶ my %params = @_ == 1 ? ( error => $_[0] ) : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 46 118 164
149  
150 164 ▶ if (HTML::Mason::Exceptions::isa_mason_exception($params{error})) {
Branch: 1 branch
BranchTrueFalseTotal
if (HTML::Mason::Exceptions::isa_mason_exception($params{"error"})) 12 152 164
151 12 $params{error} = $params{error}->error;
152 }
153 164 ▶ if (HTML::Mason::Exceptions::isa_mason_exception($params{message})) {
Branch: 1 branch
BranchTrueFalseTotal
if (HTML::Mason::Exceptions::isa_mason_exception($params{"message"})) 0 164 164
154 0 $params{message} = $params{message}->error;
155 }
156 164 $class->SUPER::throw(%params);
157 }
158  
159 sub filtered_frames
160 {
161 121 ▶ my ($self) = @_;
Subroutine filtered_frames: called CC=8 SCAR=20.8
Pod undocumented
162  
163 121 my (@frames);
164 121 my $trace = $self->trace;
165 121 my %ignore_subs = map { $_ => 1 }
166 qw[
167 (eval)
168 Exception::Class::Base::throw
169 Exception::Class::__ANON__
170 HTML::Mason::Commands::__ANON__
171 HTML::Mason::Component::run
172 HTML::Mason::Exception::throw
173 HTML::Mason::Exceptions::__ANON__
174 HTML::Mason::Request::_run_comp
175 ];
176 121 ▶ while (my $frame = $trace->next_frame)
Branch: 1 branch
BranchTrueFalseTotal
if (my $frame = $trace->next_frame) 2007 66 2073
177 {
178 2007 ▶ last if ($frame->subroutine eq 'HTML::Mason::Request::exec');
Branch: 1 branch
BranchTrueFalseTotal
if $frame->subroutine eq "HTML::Mason::Request::exec" 55 1952 2007
179 1952 ▶ unless ($frame->filename =~ /Mason\/Exceptions\.pm/ or
Branch: 1 branch
BranchTrueFalseTotal
unless ($frame->filename =~ m[Mason/Exceptions\.pm] or $ignore_subs{$frame->subroutine} or $frame->subroutine eq "HTML::Mason::Request::comp" and $frame->filename =~ /Request\.pm/) 1101 851 1952
Condition100%9/9
$frame->filename =~ m[Mason/Exceptions\.pm] or $ignore_subs{$frame->subroutine} or $frame->subroutine eq "HTML::Mason::Request::comp" and $frame->filename =~ /Request\.pm/
l!l&&r!l&&!r
801510641
$frame->filename =~ m[Mason/Exceptions\.pm] or $ignore_subs{$frame->subroutine}
l!l&&r!l&&!r
567451151
$frame->subroutine eq "HTML::Mason::Request::comp" and $frame->filename =~ /Request\.pm/
!ll&&!rl&&r
10416050
MC/DC75%3/4
$frame->filename =~ m[Mason/Exceptions\.pm] or $ignore_subs{$frame->subroutine} or $frame->subroutine eq "HTML::Mason::Request::comp" and $frame->filename =~ /Request\.pm/
$frame->filename =~ m[Mason/Exceptions\.pm] $ignore_subs{$frame->subroutine} $frame->subroutine eq "HTML::Mason::Request::comp" $frame->filename =~ /Request\.pm/
Truth table80%4/5
A or B or C and D
A= $frame->filename =~ m[Mason/Exceptions\.pm]B= $ignore_subs{$frame->subroutine}C= $frame->subroutine eq "HTML::Mason::Request::comp"D= $frame->filename =~ /Request\.pm/
ABCDresult
1XXX1
01XX1
00111
000X0
00100
180 $ignore_subs{ $frame->subroutine } or
181 ($frame->subroutine eq 'HTML::Mason::Request::comp' and $frame->filename =~ /Request\.pm/)) {
182 1101 push(@frames, $frame);
183 }
184 }
185 121 ▶ @frames = grep { $_->filename !~ /Mason\/Exceptions\.pm/ } $trace->frames if !@frames;
Branch: 1 branch
BranchTrueFalseTotal
unless @frames 3 118 121
186 121 return @frames;
187 }
188  
189 sub analyze_error
190 {
191 129 ▶ my ($self) = @_;
Subroutine analyze_error: called CC=7 SCAR=19.5
Pod undocumented
192 129 my ($file, @lines, @frames);
193  
194 129 ▶ return $self->{_info} if $self->{_info};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_info"} 8 121 129
195  
196 121 @frames = $self->filtered_frames;
197 121 ▶ if ($self->isa('HTML::Mason::Exception::Syntax')) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->isa('HTML::Mason::Exception::Syntax')) { } 16 105 121
elsif ($self->isa('HTML::Mason::Exception::Compilation')) { } 24 81 105
elsif (@frames) { } 81 0 81
198 16 $file = $self->comp_name;
199 16 push(@lines, $self->line_number);
200 } elsif ($self->isa('HTML::Mason::Exception::Compilation')) {
201 24 $file = $self->filename;
202 24 my $msg = $self->full_message;
203 24 ▶ while ($msg =~ /at .* line (\d+)./g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg =~ /at .* line (\d+)./g) 33 24 57
204 33 push(@lines, $1);
205 }
206 } elsif (@frames) {
207 81 $file = $frames[0]->filename;
208 81 @lines = $frames[0]->line;
209 }
210 121 my @context;
211 121 ▶ @context = $self->get_file_context($file, \@lines) if @lines;
Branch: 1 branch
BranchTrueFalseTotal
if @lines 121 0 121
212  
213 $self->{_info} = {
214 121 file => $file,
215 frames => \@frames,
216 lines => \@lines,
217 context => \@context,
218 };
219 121 return $self->{_info};
220 }
221  
222 sub get_file_context
223 {
224 121 ▶ my ($self, $file, $line_nums) = @_;
Subroutine get_file_context: called CC=10 SCAR=23.0
Pod undocumented
225  
226 121 my @context;
227 121 my $fh = do { local *FH; *FH; };
228 121 ▶ unless (defined($file) and open($fh, $file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file and open $fh, $file) { } 106 15 121
Condition100%3/3
defined $file and open $fh, $file
!ll&&!rl&&r
123106
MC/DC100%2/2
defined $file and open $fh, $file
defined $file open $fh, $file
Truth table100%3/3
A and B
A= defined $fileB= open $fh, $file
ABresult
0X0
100
111
229 15 @context = (['unable to open file', '']);
230 } else {
231 # Put the file into a list, indexed at 1.
232 106 my @file = <$fh>;
233 106 chomp(@file);
234 106 unshift(@file, undef);
235  
236 # Mark the important context lines.
237 # We do this by going through the error lines and incrementing hash keys to
238 # keep track of which lines we eventually need to print, and we color the
239 # line which the error actually occured on in red.
240 106 my (%marks, %red);
241 106 my $delta = 4;
242 106 foreach my $line_num (@$line_nums) {
243 111 foreach my $l (($line_num - $delta) .. ($line_num + $delta)) {
244 999 ▶ next if ($l <= 0 or $l > @file);
Branch: 1 branch
BranchTrueFalseTotal
if $l <= 0 or $l > @file 454 545 999
Condition100%3/3
$l <= 0 or $l > @file
l!l&&r!l&&!r
189265545
MC/DC100%2/2
$l <= 0 or $l > @file
$l <= 0 $l > @file
Truth table100%3/3
A or B
A= $l <= 0B= $l > @file
ABresult
1X1
011
000
245 545 $marks{$l}++;
246 }
247 111 $red{$line_num} = 1;
248 }
249  
250 # Create the context list.
251 # By going through the keys of the %marks hash, we can tell which lines need
252 # to be printed. We add a '...' line if we skip numbers in the context.
253 106 my $last_num = 0;
254 106 foreach my $line_num (sort { $a <=> $b } keys %marks) {
255 526 ▶ push(@context, ["...", "", 0]) unless $last_num == ($line_num - 1);
Branch: 1 branch
BranchTrueFalseTotal
unless $last_num == $line_num - 1 35 491 526
256 526 push(@context, ["$line_num:", $file[$line_num], $red{$line_num}]);;
257 526 $last_num = $line_num;
258 }
259 106 ▶ push(@context, ["...", "", 0]) unless $last_num == @file;
Branch: 1 branch
BranchTrueFalseTotal
unless $last_num == @file 39 67 106
260 106 close $fh;
261 }
262 121 return @context;
263 }
264  
265 # basically the same as as_string in Exception::Class::Base
266 sub raw_text
267 {
268 2 ▶ my ($self) = @_;
Subroutine raw_text: called CC=1 SCAR=0.0
Pod undocumented
269  
270 2 return $self->full_message . "\n\n" . $self->trace->as_string;
271 }
272  
273 sub as_string
274 {
275 132 ▶ my ($self) = @_;
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
276  
277 132 my $stringify_function = "as_" . $self->{format};
278 132 return $self->$stringify_function();
279 }
280  
281 sub as_brief
282 {
283 1 ▶ my ($self) = @_;
Subroutine as_brief: called CC=1 SCAR=0.0
Pod undocumented
284 1 return $self->full_message;
285 }
286  
287 sub as_line
288 {
289 0 ▶ my ($self) = @_;
Subroutine as_line: not called CC=1 SCAR=6.9
Pod undocumented
290 0 my $info = $self->analyze_error;
291  
292 0 (my $msg = $self->full_message) =~ s/\n/\t/g;
293 0 my $stack = join(", ", map { sprintf("[%s:%d]", $_->filename, $_->line) } @{$info->{frames}});
294 0 return sprintf("%s\tStack: %s\n", $msg, $stack);
295 }
296  
297 sub as_text
298 {
299 127 ▶ my ($self) = @_;
Subroutine as_text: called CC=1 SCAR=0.0
Pod undocumented
300 127 my $info = $self->analyze_error;
301  
302 127 my $msg = $self->full_message;
303 127 my $stack = join("\n", map { sprintf(" [%s:%d]", $_->filename, $_->line) } @{$info->{frames}});
304 127 return sprintf("%s\nStack:\n%s\n", $msg, $stack);
305 }
306  
307 sub as_html
308 {
309 2 ▶ my ($self) = @_;
Subroutine as_html: called CC=1 SCAR=0.0
Pod undocumented
310  
311 2 my $out;
312 2 my $interp = HTML::Mason::Interp->new(out_method => \$out);
313  
314 2 my $comp = $interp->make_component(comp_source => <<'EOF');
315  
316 <%args>
317 $msg
318 $info
319 $error
320 </%args>
321 <%filter>
322 s/(<td [^\>]+>)/$1<font face="Verdana, Arial, Helvetica, sans-serif" size="-2">/g;
323 s/<\/td>/<\/font><\/td>/g;
324 </%filter>
325  
326 % HTML::Mason::Escapes::basic_html_escape(\$msg);
327 % $msg =~ s/\n/<br>/g;
328  
329 <html><body>
330  
331 <p align="center"><font face="Verdana, Arial, Helvetica, sans-serif"><b>System error</b></font></p>
332 <table border="0" cellspacing="0" cellpadding="1">
333 <tr>
334 <td nowrap="nowrap" align="left" valign="top"><b>error:</b>&nbsp;</td>
335 <td align="left" valign="top"><% $msg %></td>
336 </tr>
337 <tr>
338 <td nowrap="nowrap" align="left" valign="top"><b>context:</b>&nbsp;</td>
339 <td align="left" valign="top" nowrap="nowrap">
340 <table border="0" cellpadding="0" cellspacing="0">
341  
342 % foreach my $entry (@{$info->{context}}) {
343 % my ($line_num, $line, $highlight) = @$entry;
344 % $line = '' unless defined $line;
345 % HTML::Mason::Escapes::basic_html_escape(\$line);
346 <tr>
347 <td nowrap="nowrap" align="left" valign="top"><b><% $line_num %></b>&nbsp;</td>
348 <td align="left" valign="top" nowrap="nowrap"><% $highlight ? "<font color=red>" : "" %><% $line %><% $highlight ? "</font>" : "" %></td>
349 </tr>
350  
351 % }
352  
353 </table>
354 </td>
355 </tr>
356 <tr>
357 <td align="left" valign="top" nowrap="nowrap"><b>code stack:</b>&nbsp;</td>
358 <td align="left" valign="top" nowrap="nowrap">
359 % foreach my $frame (@{$info->{frames}}) {
360 % my $f = $frame->filename; HTML::Mason::Escapes::basic_html_escape(\$f);
361 % my $l = $frame->line; HTML::Mason::Escapes::basic_html_escape(\$l);
362 <% $f %>:<% $l %><br>
363 % }
364 </td>
365 </tr>
366 </table>
367  
368 <a href="#raw">raw error</a><br>
369  
370 <br>
371 <br>
372 <br>
373 <br>
374 <br>
375 <br>
376 <br>
377 <br>
378 <br>
379 <br>
380 <br>
381 <br>
382 <br>
383 <br>
384 <br>
385 <br>
386 <br>
387 <br>
388 <br>
389 <br>
390 <br>
391 <br>
392 <br>
393 <br>
394 <br>
395 <br>
396 <br>
397 <br>
398 <br>
399 <br>
400  
401 % my $raw = $error->raw_text;
402 % HTML::Mason::Escapes::basic_html_escape(\$raw);
403 % $raw =~ s/\t//g;
404  
405 <a name="raw"></a>
406  
407 <pre><% $raw %></pre>
408  
409 </body></html>
410 EOF
411  
412 2 $interp->exec($comp,
413 msg => $self->full_message,
414 info => $self->analyze_error,
415 error => $self);
416  
417 2 return $out;
418 }
419  
420 package HTML::Mason::Exception::Compilation;
421 34 $HTML::Mason::Exception::Compilation::VERSION = '1.58';
422 sub full_message
423 {
424 48 ▶ my $self = shift;
Subroutine full_message: called CC=3 SCAR=11.1
Pod documented
425  
426 48 ▶ return sprintf("Error during compilation of %s:\n%s\n", $self->filename || '', $self->message || '');
Condition75%3/4
$self->filename || ''
l!l
2424
$self->message || ''
l!l
480
MC/DC100%1/1
$self->filename || ''
$self->filename
MC/DC0%0/1
$self->message || ''
$self->message
Truth table100%2/2
A
A= $self->filename
Aresult
11
00
Truth table50%1/2
A
A= $self->message
Aresult
11
00
427 }
428  
429 package HTML::Mason::Exception::Syntax;
430 34 $HTML::Mason::Exception::Syntax::VERSION = '1.58';
431 sub full_message
432 {
433 16 ▶ my $self = shift;
Subroutine full_message: called CC=3 SCAR=12.0
Pod documented
434  
435 16 ▶ return sprintf("%s at %s line %d", $self->message || '', $self->comp_name || '', $self->line_number);
Condition50%2/4
$self->message || ''
l!l
160
$self->comp_name || ''
l!l
160
MC/DC0%0/1
$self->message || ''
$self->message
MC/DC0%0/1
$self->comp_name || ''
$self->comp_name
Truth table50%1/2
A
A= $self->message
Aresult
11
00
Truth table50%1/2
A
A= $self->comp_name
Aresult
11
00
436 }
437  
438 34 1;
439  
440 __END__
« lib/HTML/Mason/Component.pm ↑ Summary script/mason.pl »