GX/Exception.pm

filter: Statementstmt 88.8% 144 / 162 Branchbran 69.5% 57 / 82 Conditioncond 38.7% 12 / 31 MC/DCmcdc 15.7% 3 / 19 Subroutinesub 93.1% 27 / 29 Podpod 100% 8 / 8 totaltotal 75.8% 251 / 331 CC 54 SCAR 21.8
CC
54
Cov
77%
subexception
24.4
stack_trace
19.6
new
15.1
CRAP 8.8
« GX/Meta/Attribute/Hash.pm ↑ Summary GX/Meta/Class.pm »
1 # ----------------------------------------------------------------------------------------------------------------------
2 # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de>
3 # File: GX/Exception.pm
4 # ----------------------------------------------------------------------------------------------------------------------
5  
6 package GX::Exception;
7  
8 304 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 304 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 304 ▶use GX::Exception::StackTrace;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 304 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 # ----------------------------------------------------------------------------------------------------------------------
17 # Export mechanism
18 # ----------------------------------------------------------------------------------------------------------------------
19  
20 {
21  
22 my %EXPORTS = (
23  
24 3 ▶ 'catch' => sub (&) {
Subroutine __ANON__: called CC=1 SCAR=0.0
25 3 return $_[0];
26 },
27  
28 46 ▶ 'try' => sub (&;$) {
Subroutine __ANON__: called CC=7 SCAR=19.5
29  
30 46 my $try = shift;
31 46 my $catch = shift;
32  
33 46 my $wantarray = wantarray;
34  
35 46 my ( @result, $died, $error );
36  
37 {
38  
39 46 local $@;
40  
41 eval {
42  
43 46 ▶ if ( $wantarray ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($wantarray) { } 1 45 46
elsif (defined $wantarray) { } 4 41 45
44 1 @result = $try->();
45 } elsif ( defined $wantarray ) {
46 4 $result[0] = $try->();
47 } else {
48 41 $try->();
49 };
50  
51 43 1;
52  
53 46 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { if ($wantarray) { @result = &$try(); } elsif (defined $wantarray) { $result[0] = &$try(); } else { &$try(); } ; 1 } } or do { $died = 1; $error = $@ } 43 3 46
54 3 $died = 1;
55 3 $error = $@;
56 };
57  
58 }
59  
60 46 ▶ if ( $died ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($died) 3 43 46
61  
62 3 ▶ if ( $catch ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($catch) 2 1 3
63 2 local $_ = $error;
64 2 return $catch->( $error );
65 }
66  
67 1 return;
68  
69 }
70  
71 43 ▶ return $wantarray ? @result : $result[0];
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 1 42 43
72  
73 }
74  
75 304 );
76  
77 4131 ▶ sub import {
Subroutine import: called CC=2 SCAR=6.9
78  
79 4131 my $class = shift;
80  
81 4131 my $caller = caller();
82  
83 {
84  
85 304 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
86  
87 4131 ▶ *{"$caller\::complain"} = sub (*) {
Subroutine __ANON__: called CC=1 SCAR=0.0
88 451 unshift( @_, $class );
89 451 goto &complain;
90 4131 };
91  
92 4131 ▶ *{"$caller\::throw"} = sub (*) {
Subroutine __ANON__: called CC=1 SCAR=0.0
93 30 unshift( @_, $class );
94 30 goto &throw;
95 4131 };
96  
97 4131 ▶ while ( my ( $name, $code ) = each %EXPORTS ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %EXPORTS) 8262 4131 12393
98 8262 *{"$caller\::$name"} = $code;
99 }
100  
101 }
102  
103 4131 return;
104  
105 }
106  
107 }
108  
109  
110 # ----------------------------------------------------------------------------------------------------------------------
111 # Overloading
112 # ----------------------------------------------------------------------------------------------------------------------
113  
114 use overload
115 283 ▶ 'bool' => sub { 1 },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
116 0 ▶ '0+' => sub { $_[0] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
117 67 ▶ '""' => sub { scalar $_[0]->_as_string },
Subroutine __ANON__: called CC=1 SCAR=0.0
118 304 'fallback' => 1;
119  
120  
121 # ----------------------------------------------------------------------------------------------------------------------
122 # Constructor
123 # ----------------------------------------------------------------------------------------------------------------------
124  
125 779 ▶sub new {
Subroutine new: called CC=14 SCAR=27.2
Pod documented
126  
127 779 my $class = shift;
128  
129 779 my $message;
130 my $stack_trace;
131 779 my $subexception;
132 779 my $verbosity;
133  
134 779 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 768 11 779
135  
136 768 ▶ if ( blessed $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($_[0])) { } 374 394 768
137  
138 374 ▶ if ( $_[0]->isa( __PACKAGE__ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]->isa(__PACKAGE__)) { } 374 0 374
139 374 $message = $_[0]->message;
140 374 $subexception = $_[0]->subexception;
141 }
142 else {
143 0 $message = "$_[0]";
144 }
145  
146 }
147 else {
148 394 ▶ $message = defined $_[0] ? "$_[0]" : $class->_default_message;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 394 0 394
149 }
150  
151 768 $verbosity = $class->_default_verbosity;
152  
153 }
154 else {
155  
156 11 my %args = @_;
157  
158 11 ▶ if ( defined $args{'message'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'message'}) { } 3 8 11
159 3 $message = $args{'message'};
160 }
161 else {
162 8 $message = $class->_default_message;
163 }
164  
165 11 ▶ if ( blessed $args{'stack_trace'} && $args{'stack_trace'}->isa( 'GX::Exception::StackTrace' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($args{"stack_trace"}) and $args{"stack_trace"}->isa("GX::Exception::StackTrace")) 0 11 11
Condition33%1/3
builtin::blessed($args{"stack_trace"}) and $args{"stack_trace"}->isa("GX::Exception::StackTrace")
!ll&&!rl&&r
1100
MC/DC0%0/2
builtin::blessed($args{"stack_trace"}) and $args{"stack_trace"}->isa("GX::Exception::StackTrace")
builtin::blessed($args{"stack_trace"}) $args{"stack_trace"}->isa("GX::Exception::StackTrace")
Truth table33%1/3
A and B
A= builtin::blessed($args{"stack_trace"})B= $args{"stack_trace"}->isa("GX::Exception::StackTrace")
ABresult
0X0
100
111
166 0 $stack_trace = $args{'stack_trace'};
167 }
168  
169 11 ▶ if ( defined $args{'subexception'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"subexception"}) 3 8 11
170  
171 3 ▶ if ( blessed $args{'subexception'} && $args{'subexception'}->isa( __PACKAGE__ ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($args{'subexception'}) and $args{'subexception'}->isa(__PACKAGE__)) { } 2 1 3
elsif (length $args{'subexception'}) { } 1 0 1
Condition66%2/3
builtin::blessed($args{"subexception"}) and $args{"subexception"}->isa(__PACKAGE__)
!ll&&!rl&&r
102
MC/DC50%1/2
builtin::blessed($args{"subexception"}) and $args{"subexception"}->isa(__PACKAGE__)
builtin::blessed($args{"subexception"}) $args{"subexception"}->isa(__PACKAGE__)
Truth table66%2/3
A and B
A= builtin::blessed($args{"subexception"})B= $args{"subexception"}->isa(__PACKAGE__)
ABresult
0X0
100
111
172 2 $subexception = $args{'subexception'};
173 }
174 elsif ( length $args{'subexception'} ) {
175 1 $subexception = __PACKAGE__->new( $args{'subexception'} );
176 }
177  
178 }
179  
180 11 ▶ if ( defined $args{'verbosity'} && $args{'verbosity'} =~ /^(?:0|1|2|3)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'verbosity'} and $args{'verbosity'} =~ /^(?:0|1|2|3)$/) { } 1 10 11
Condition66%2/3
defined $args{"verbosity"} and $args{"verbosity"} =~ /^(?:0|1|2|3)$/
!ll&&!rl&&r
1001
MC/DC50%1/2
defined $args{"verbosity"} and $args{"verbosity"} =~ /^(?:0|1|2|3)$/
defined $args{"verbosity"} $args{"verbosity"} =~ /^(?:0|1|2|3)$/
Truth table66%2/3
A and B
A= defined $args{"verbosity"}B= $args{"verbosity"} =~ /^(?:0|1|2|3)$/
ABresult
0X0
100
111
181 1 $verbosity = $args{'verbosity'};
182 }
183 else {
184 10 $verbosity = $class->_default_verbosity;
185 }
186  
187 }
188  
189 779 my $self = bless {
190 'message' => $message,
191 'stack_trace' => $stack_trace,
192 'subexception' => $subexception,
193 'verbosity' => $verbosity
194 }, $class;
195  
196 779 return $self;
197  
198 }
199  
200  
201 # ----------------------------------------------------------------------------------------------------------------------
202 # Public methods
203 # ----------------------------------------------------------------------------------------------------------------------
204  
205 0 ▶sub as_string {
Subroutine as_string: not called CC=1 SCAR=6.9
Pod documented
206  
207 0 return shift->_as_string( @_ );
208  
209 }
210  
211 730 ▶sub complain {
Subroutine complain: called CC=2 SCAR=6.9
Pod documented
212  
213 730 ▶ my $self = blessed $_[0] ? shift : shift->new( @_ );
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) ? : 1 729 730
214  
215 730 $self->_update_stack_trace( 1 );
216  
217 730 die $self;
218  
219 }
220  
221 491 ▶sub message {
Subroutine message: called CC=3 SCAR=14.2
Pod documented
222  
223 491 my $self = shift;
224  
225 491 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 491 491
226 0 ▶ return $self->{'message'} = defined $_[0] ? "$_[0]" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
227 }
228  
229 491 return $self->{'message'};
230  
231 }
232  
233 235 ▶sub stack_trace {
Subroutine stack_trace: called CC=6 SCAR=29.7
Pod documented
234  
235 235 my $self = shift;
236  
237 235 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 235 235
238  
239 0 ▶ if ( blessed $_[0] && $_[0]->isa( 'GX::Exception::StackTrace' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($_[0]) and $_[0]->isa('GX::Exception::StackTrace')) { } 0 0 0
Condition0%0/3
builtin::blessed($_[0]) and $_[0]->isa("GX::Exception::StackTrace")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($_[0]) and $_[0]->isa("GX::Exception::StackTrace")
builtin::blessed($_[0]) $_[0]->isa("GX::Exception::StackTrace")
Truth table0%0/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa("GX::Exception::StackTrace")
ABresult
0X0
100
111
240 0 $self->{'subexception'} = $_[0];
241 }
242 else {
243 0 $self->{'subexception'} = undef;
244 }
245  
246 }
247  
248 0 ▶ return wantarray ? @{ $self->{'stack_trace'} || return } : $self->{'stack_trace'};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 235 235
Condition0%0/2
$self->{'stack_trace'} || (return)
l!l
00
MC/DC0%0/1
$self->{'stack_trace'} || (return)
$self->{'stack_trace'}
Truth table0%0/2
A
A= $self->{'stack_trace'}
Aresult
11
00
249  
250 }
251  
252 379 ▶sub subexception {
Subroutine subexception: called CC=6 SCAR=32.0
Pod documented
253  
254 379 my $self = shift;
255  
256 379 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 379 379
257  
258 0 ▶ if ( defined $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 0 0 0
259  
260 0 ▶ if ( blessed $_[0] && $_[0]->isa( __PACKAGE__ ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)) { } 0 0 0
elsif (length $_[0]) { } 0 0 0
Condition0%0/3
builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__)
Truth table0%0/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa(__PACKAGE__)
ABresult
0X0
100
111
261 0 $self->{'subexception'} = $_[0];
262 }
263 elsif ( length $_[0] ) {
264 0 $self->{'subexception'} = __PACKAGE__->new( $_[0] );
265 }
266 else {
267 0 $self->{'subexception'} = undef;
268 }
269  
270 }
271 else {
272 0 $self->{'subexception'} = undef;
273 }
274  
275 }
276  
277 379 return $self->{'subexception'};
278  
279 }
280  
281 45 ▶sub throw {
Subroutine throw: called CC=2 SCAR=6.9
Pod documented
282  
283 45 ▶ my $self = blessed $_[0] ? shift : shift->new( @_ );
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) ? : 1 44 45
284  
285 45 $self->_update_stack_trace;
286  
287 45 die $self;
288  
289 }
290  
291 70 ▶sub verbosity {
Subroutine verbosity: called CC=4 SCAR=14.5
Pod documented
292  
293 70 my $self = shift;
294  
295 70 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 67 70
296  
297 3 ▶ if ( defined $_[0] && $_[0] =~ /^(?:0|1|2|3)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0] and $_[0] =~ /^(?:0|1|2|3)$/) 3 0 3
Condition33%1/3
defined $_[0] and $_[0] =~ /^(?:0|1|2|3)$/
!ll&&!rl&&r
003
MC/DC0%0/2
defined $_[0] and $_[0] =~ /^(?:0|1|2|3)$/
defined $_[0] $_[0] =~ /^(?:0|1|2|3)$/
Truth table33%1/3
A and B
A= defined $_[0]B= $_[0] =~ /^(?:0|1|2|3)$/
ABresult
0X0
100
111
298 3 $self->{'verbosity'} = $_[0];
299 }
300  
301 }
302  
303 70 return $self->{'verbosity'};
304  
305 }
306  
307  
308 # ----------------------------------------------------------------------------------------------------------------------
309 # Private methods
310 # ----------------------------------------------------------------------------------------------------------------------
311  
312 67 ▶sub _as_string {
Subroutine _as_string: called CC=9 SCAR=22.8
313  
314 67 my $self = shift;
315 67 my $verbosity = shift;
316  
317 67 ▶ $verbosity //= $self->verbosity;
Condition33%1/3
$verbosity //= $self->verbosity
l!l&&r!l&&!r
0670
MC/DC0%0/2
$verbosity //= $self->verbosity
$verbosity $self->verbosity
Truth table33%1/3
A //= B
A= $verbosityB= $self->verbosity
ABresult
1X1
011
000
318  
319 67 my @lines;
320  
321 67 push @lines, $self->_message_as_string;
322  
323 67 ▶ if ( $verbosity > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbosity > 0) 4 63 67
324  
325 4 ▶ if ( $verbosity == 1 ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($verbosity == 1) { } 2 2 4
elsif ($verbosity == 2) { } 1 1 2
elsif ($verbosity == 3) { } 1 0 1
326  
327 2 my $exception = $self;
328  
329 2 ▶ while ( $exception = $exception->subexception ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exception = $exception->subexception) 2 2 4
330 2 push @lines, $exception->_message_as_string;
331 }
332  
333 }
334 elsif ( $verbosity == 2 ) {
335  
336 1 push @lines, $self->_stack_trace_as_string( 1 );
337  
338 }
339 elsif ( $verbosity == 3 ) {
340  
341 1 push @lines, $self->_stack_trace_as_string( 1 );
342  
343 {
344  
345 1 my $level = 0;
346 1 my $exception = $self;
347  
348 1 ▶ while ( $exception = $exception->subexception ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exception = $exception->subexception) 0 1 1
349 0 my $indentation = ' ' x ( 4 * ++$level );
350 0 push @lines, map { $indentation . $_ } $exception->_as_string( 1 );
351 }
352  
353 }
354  
355 }
356  
357 }
358  
359 67 ▶ return wantarray ? @lines : join( "\n", @lines ) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 67 67
360  
361 }
362  
363 775 ▶sub _build_stack_trace {
Subroutine _build_stack_trace: called CC=2 SCAR=7.0
364  
365 775 my $self = shift;
366 775 my $offset = shift;
367  
368 775 my $stack_trace = GX::Exception::StackTrace->new;
369  
370 775 ▶ $stack_trace->build( ( $offset || 0 ) + 1 );
Condition50%1/2
$offset || 0
l!l
7750
MC/DC0%0/1
$offset || 0
$offset
Truth table50%1/2
A
A= $offset
Aresult
11
00
371  
372 775 return $stack_trace;
373  
374 }
375  
376 8 ▶sub _default_message {
Subroutine _default_message: called CC=1 SCAR=0.0
377  
378 8 return 'Unknown error';
379  
380 }
381  
382 778 ▶sub _default_verbosity {
Subroutine _default_verbosity: called CC=1 SCAR=0.0
383  
384 778 return 0;
385  
386 }
387  
388 69 ▶sub _message_as_string {
Subroutine _message_as_string: called CC=6 SCAR=18.7
389  
390 69 my $self = shift;
391  
392 69 my $string = $self->message;
393  
394 69 ▶ if ( $string !~ s/\n$// ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($string =~ s/\n$//) 13 56 69
395  
396 13 ▶ if ( my $stack_trace = $self->stack_trace ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $stack_trace = $self->stack_trace) 13 0 13
397  
398 13 ▶ if ( my $frame = $stack_trace->[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $frame = $stack_trace->[0]) 13 0 13
399 13 ▶ $string .= sprintf(
Condition50%2/4
$frame->filename // "?"
l!l
130
$frame->line // "?"
l!l
130
MC/DC0%0/1
$frame->filename // "?"
$frame->filename
MC/DC0%0/1
$frame->line // "?"
$frame->line
Truth table50%1/2
A
A= $frame->filename
Aresult
11
00
Truth table50%1/2
A
A= $frame->line
Aresult
11
00
400 " at %s line %s.",
401 $frame->filename // '?',
402 $frame->line // '?'
403 );
404 }
405  
406 }
407  
408 }
409  
410 69 return $string;
411  
412 }
413  
414 2 ▶sub _stack_trace_as_string {
Subroutine _stack_trace_as_string: called CC=2 SCAR=7.0
415  
416 2 my $self = shift;
417 2 my $offset = shift;
418  
419 2 my $stack_trace = $self->stack_trace;
420  
421 2 ▶ return $stack_trace ? $stack_trace->as_string( $offset ) : '';
Branch: 1 branch
BranchTrueFalseTotal
$stack_trace ? : 2 0 2
422  
423 }
424  
425 775 ▶sub _update_stack_trace {
Subroutine _update_stack_trace: called CC=2 SCAR=6.9
426  
427 775 my $self = shift;
428 775 my $offset = shift;
429  
430 775 ▶ $offset //= 0;
Condition100%2/2
$offset //= 0
l!l
73045
MC/DC100%1/1
$offset //= 0
$offset
Truth table100%2/2
A
A= $offset
Aresult
11
00
431  
432 775 $self->{'stack_trace'} = $self->_build_stack_trace( $offset + 2 );
433  
434 775 return;
435  
436 }
437  
438  
439 304 1;
440  
441 __END__
« GX/Meta/Attribute/Hash.pm ↑ Summary GX/Meta/Class.pm »