Class/Base.pm

filter: Statementstmt 93.6% 117 / 125 Branchbran 71.7% 33 / 46 Conditioncond 50.0% 10 / 20 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 92.8% 26 / 28 Podpod 76.9% 10 / 13 totaltotal 81.8% 199 / 243 CC 32 SCAR 17.1
CC
32
Cov
84%
_initialize
13.1
as_string
11.2
show_trace
4.3
CRAP 5.5
« Class.pm ↑ Summary
1 package Exception::Class::Base;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 our $VERSION = '1.43';
7  
8 5 ▶use Class::Data::Inheritable 0.02;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Devel::StackTrace 2.00;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 ▶use base qw(Class::Data::Inheritable);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 BEGIN {
15 5 ▶ __PACKAGE__->mk_classdata('Trace');
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 __PACKAGE__->mk_classdata('UnsafeRefCapture');
17  
18 5 __PACKAGE__->mk_classdata('NoContextInfo');
19 5 __PACKAGE__->NoContextInfo(0);
20  
21 5 __PACKAGE__->mk_classdata('RespectOverload');
22 5 __PACKAGE__->RespectOverload(0);
23  
24 5 __PACKAGE__->mk_classdata('MaxArgLength');
25 5 __PACKAGE__->MaxArgLength(0);
26  
27 sub NoRefs {
28 1 ▶ my $self = shift;
Subroutine NoRefs: called CC=2 SCAR=7.4
Pod undocumented
29 1 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1 0 1
30 1 my $val = shift;
31 1 return $self->UnsafeRefCapture( !$val );
32 }
33 else {
34 0 return $self->UnsafeRefCapture;
35 }
36 }
37  
38 38 ▶ sub Fields { () }
Subroutine Fields: called CC=1 SCAR=0.0
Pod documented
39 }
40  
41 use overload
42  
43 # an exception is always true
44 5 ▶ bool => sub {1}, '""' => 'as_string', fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
45  
46 # Create accessor routines
47 BEGIN {
48 5 ▶ my @fields = qw( message pid uid euid gid egid time trace );
Subroutine BEGIN: called CC=3 SCAR=11.0
49  
50 5 foreach my $f (@fields) {
51 40 ▶ my $sub = sub { my $s = shift; return $s->{$f}; };
Subroutine __ANON__: called CC=1 SCAR=0.0
52  
53 ## no critic (TestingAndDebugging::ProhibitNoStrict)
54 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
55 40 *{$f} = $sub;
56 }
57 5 *error = \&message;
58  
59 5 my %trace_fields = (
60 package => 'package',
61 file => 'filename',
62 line => 'line',
63 );
64  
65 5 ▶ while ( my ( $f, $m ) = each %trace_fields ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %trace_fields) 15 5 20
66 my $sub = sub {
67 4 ▶ my $s = shift;
Subroutine __ANON__: called CC=3 SCAR=11.4
68 4 ▶ return $s->{$f} if exists $s->{$f};
Branch: 1 branch
BranchTrueFalseTotal
if exists $s->{$f} 0 4 4
69  
70 4 my $frame = $s->trace->frame(0);
71  
72 4 ▶ return $s->{$f} = $frame ? $frame->$m : undef;
Branch: 1 branch
BranchTrueFalseTotal
$frame ? : 4 0 4
73 15 };
74  
75 ## no critic (TestingAndDebugging::ProhibitNoStrict)
76 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
77 15 *{$f} = $sub;
78 }
79 }
80  
81 0 ▶sub Classes { Exception::Class::Classes() }
Subroutine Classes: not called CC=1 SCAR=6.9
Pod undocumented
82  
83 sub throw {
84 37 ▶ my $proto = shift;
Subroutine throw: called CC=2 SCAR=7.1
Pod documented
85  
86 37 ▶ $proto->rethrow if ref $proto;
Branch: 1 branch
BranchTrueFalseTotal
if ref $proto 0 37 37
87  
88 37 die $proto->new(@_);
89 }
90  
91 sub rethrow {
92 0 ▶ my $self = shift;
Subroutine rethrow: not called CC=1 SCAR=6.9
Pod documented
93  
94 0 die $self;
95 }
96  
97 sub new {
98 37 ▶ my $proto = shift;
Subroutine new: called CC=2 SCAR=7.2
Pod documented
99 37 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0370
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
100  
101 37 my $self = bless {}, $class;
102  
103 37 $self->_initialize(@_);
104  
105 37 return $self;
106 }
107  
108 sub _initialize {
109 37 ▶ my $self = shift;
Subroutine _initialize: called CC=13 SCAR=25.7
110 37 ▶ my %p = @_ == 1 ? ( error => $_[0] ) : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 4 33 37
111  
112 37 ▶ $self->{message} = $p{message} || $p{error} || q{};
Condition100%5/5
$p{'message'} || $p{'error'} || ''
l!l
325
$p{"message"} or $p{"error"}
l!l&&r!l&&!r
1315
MC/DC100%2/2
$p{'message'} || $p{'error'} || ''
$p{"message"} $p{"error"}
Truth table100%3/3
A or B
A= $p{"message"}B= $p{"error"}
ABresult
1X1
011
000
113  
114 37 ▶ $self->{show_trace} = $p{show_trace} if exists $p{show_trace};
Branch: 1 branch
BranchTrueFalseTotal
if exists $p{"show_trace"} 2 35 37
115  
116 37 ▶ if ( $self->NoContextInfo ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->NoContextInfo) { } 1 36 37
117 1 $self->{show_trace} = 0;
118 1 $self->{package} = $self->{file} = $self->{line} = undef;
119 }
120 else {
121 # CORE::time is important to fix an error with some versions of
122 # Perl
123 36 $self->{time} = CORE::time();
124 36 $self->{pid} = $$;
125 36 $self->{uid} = $<;
126 36 $self->{euid} = $>;
127 36 $self->{gid} = $(;
128 36 $self->{egid} = $);
129  
130 36 my @ignore_class = (__PACKAGE__);
131 36 my @ignore_package = 'Exception::Class';
132  
133 36 ▶ if ( my $i = delete $p{ignore_class} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $i = delete $p{"ignore_class"}) 1 35 36
134 1 ▶ push @ignore_class, ( ref($i) eq 'ARRAY' ? @$i : $i );
Branch: 1 branch
BranchTrueFalseTotal
ref $i eq 'ARRAY' ? : 1 0 1
135 }
136  
137 36 ▶ if ( my $i = delete $p{ignore_package} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $i = delete $p{"ignore_package"}) 2 34 36
138 2 ▶ push @ignore_package, ( ref($i) eq 'ARRAY' ? @$i : $i );
Branch: 1 branch
BranchTrueFalseTotal
ref $i eq 'ARRAY' ? : 2 0 2
139 }
140  
141 36 $self->{trace} = Devel::StackTrace->new(
142 ignore_class => \@ignore_class,
143 ignore_package => \@ignore_package,
144 unsafe_ref_capture => $self->UnsafeRefCapture,
145 respect_overload => $self->RespectOverload,
146 max_arg_length => $self->MaxArgLength,
147 );
148 }
149  
150 37 my %fields = map { $_ => 1 } $self->Fields;
151 37 ▶ while ( my ( $key, $value ) = each %p ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %p) 39 37 76
152 39 ▶ next if $key =~ /^(?:error|message|show_trace)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^(?:error|message|show_trace)$/ 34 5 39
153  
154 5 ▶ if ( $fields{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields{$key}) { } 5 0 5
155 5 $self->{$key} = $value;
156 }
157 else {
158 0 Exception::Class::Base->throw(
159 error => "unknown field $key passed to constructor for class "
160 . ref $self );
161 }
162 }
163 }
164  
165 sub context_hash {
166 1 ▶ my $self = shift;
Subroutine context_hash: called CC=1 SCAR=0.0
Pod documented
167  
168 return {
169 time => $self->{time},
170 pid => $self->{pid},
171 uid => $self->{uid},
172 euid => $self->{euid},
173 gid => $self->{gid},
174 egid => $self->{egid},
175 1 };
176 }
177  
178 sub field_hash {
179 1 ▶ my $self = shift;
Subroutine field_hash: called CC=2 SCAR=6.9
Pod documented
180  
181 1 my $hash = {};
182  
183 1 for my $field ( $self->Fields ) {
184 2 $hash->{$field} = $self->$field;
185 }
186  
187 1 return $hash;
188 }
189  
190 sub description {
191 2 ▶ return 'Generic exception';
Subroutine description: called CC=1 SCAR=0.0
Pod documented
192 }
193  
194 sub show_trace {
195 9 ▶ my $self = shift;
Subroutine show_trace: called CC=4 SCAR=14.6
Pod documented
196  
197 9 ▶ return 0 unless $self->{trace};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"trace"} 0 9 9
198  
199 9 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 9 9
200 0 $self->{show_trace} = shift;
201 }
202  
203 9 ▶ return exists $self->{show_trace} ? $self->{show_trace} : $self->Trace;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'show_trace'} ? : 2 7 9
204 }
205  
206 sub as_string {
207 9 ▶ my $self = shift;
Subroutine as_string: called CC=6 SCAR=24.2
Pod documented
208  
209 9 my $str = $self->full_message;
210 9 ▶ unless ( defined $str && length $str ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $str and length $str) 0 9 9
Condition33%1/3
defined $str and length $str
!ll&&!rl&&r
009
MC/DC0%0/2
defined $str and length $str
defined $str length $str
Truth table33%1/3
A and B
A= defined $strB= length $str
ABresult
0X0
100
111
211 0 my $desc = $self->description;
212 0 ▶ $str = defined $desc
Branch: 1 branch
BranchTrueFalseTotal
defined $desc && length $desc ? : 0 0 0
Condition0%0/3
defined $desc and length $desc
!ll&&!rl&&r
000
MC/DC0%0/2
defined $desc and length $desc
defined $desc length $desc
Truth table0%0/3
A and B
A= defined $descB= length $desc
ABresult
0X0
100
111
213 && length $desc ? "[$desc]" : '[Generic exception]';
214 }
215  
216 9 ▶ $str .= "\n\n" . $self->trace->as_string
Branch: 1 branch
BranchTrueFalseTotal
if $self->show_trace 7 2 9
217 if $self->show_trace;
218  
219 9 return $str;
220 }
221  
222 8 ▶sub full_message { $_[0]->message }
Subroutine full_message: called CC=1 SCAR=0.0
Pod documented
223  
224 #
225 # The %seen bit protects against circular inheritance.
226 #
227 ## no critic (BuiltinFunctions::ProhibitStringyEval, ErrorHandling::RequireCheckingReturnValueOfEval)
228 5 ▶eval <<'EOF' if $] == 5.006;
Branch: 1 branch
BranchTrueFalseTotal
if $] == "5.006" 0 5 5
229 sub isa {
230 my ( $inheritor, $base ) = @_;
231 $inheritor = ref($inheritor) if ref($inheritor);
232  
233 my %seen;
234  
235 no strict 'refs';
236 my @parents = ( $inheritor, @{"$inheritor\::ISA"} );
237 while ( my $class = shift @parents ) {
238 return 1 if $class eq $base;
239  
240 push @parents, grep { !$seen{$_}++ } @{"$class\::ISA"};
241 }
242 return 0;
243 }
244 EOF
245  
246 sub caught {
247 3 ▶ my $class = shift;
Subroutine caught: called CC=4 SCAR=14.5
Pod undocumented
248  
249 3 my $e = $@;
250  
251 3 ▶ return unless defined $e && blessed($e) && $e->isa($class);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $e and builtin::blessed($e) and $e->isa($class) 1 2 3
Condition50%3/6
defined $e and builtin::blessed($e) and $e->isa($class)
!ll&&!rl&&r
012
defined $e and builtin::blessed($e)
!ll&&!rl&&r
003
MC/DC33%1/3
defined $e and builtin::blessed($e) and $e->isa($class)
defined $e builtin::blessed($e) $e->isa($class)
Truth table50%2/4
A and B and C
A= defined $eB= builtin::blessed($e)C= $e->isa($class)
ABCresult
0XX0
10X0
1100
1111
252 2 return $e;
253 }
254  
255 5 1;
256  
257 # ABSTRACT: A base class for exception objects
258  
259 __END__
« Class.pm ↑ Summary