Class/Base.pm

filter: Statementstmt 93.6% 118 / 126 Branchbran 72.9% 35 / 48 Conditioncond 50.0% 10 / 20 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 92.8% 26 / 28 Podpod 76.9% 10 / 13 totaltotal 82.1% 202 / 246 CC 33 SCAR 17.7
CC
33
Cov
84%
_initialize
14.1
as_string
11.2
show_trace
4.3
CRAP 5.9
« Class.pm ↑ Summary
1 package Exception::Class::Base;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 our $VERSION = '1.46';
7  
8 7 ▶use Class::Data::Inheritable 0.02;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Devel::StackTrace 2.00;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 7 ▶use base qw(Class::Data::Inheritable);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 7 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 __PACKAGE__->mk_classdata('Trace');
16 7 __PACKAGE__->mk_classdata('UnsafeRefCapture');
17  
18 7 __PACKAGE__->mk_classdata('NoContextInfo');
19 7 __PACKAGE__->NoContextInfo(0);
20  
21 7 __PACKAGE__->mk_classdata('RespectOverload');
22 7 __PACKAGE__->RespectOverload(0);
23  
24 7 __PACKAGE__->mk_classdata('MaxArgLength');
25 7 __PACKAGE__->MaxArgLength(0);
26  
27 1 ▶ sub NoRefs {
Subroutine NoRefs: called CC=2 SCAR=7.4
Pod undocumented
28 1 my $self = shift;
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 40 ▶ 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 7 ▶ 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 7 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.0
48 7 my @fields = qw( message pid uid euid gid egid time trace );
49  
50 7 foreach my $f (@fields) {
51 56 ▶ my $sub = sub { my $s = shift; return $s->{$f}; };
Subroutine __ANON__: called CC=1 SCAR=0.0
52  
53 ## no critic (TestingAndDebugging::ProhibitNoStrict)
54 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
55 56 *{$f} = $sub;
56 }
57 7 *error = \&message;
58  
59 7 my %trace_fields = (
60 package => 'package',
61 file => 'filename',
62 line => 'line',
63 );
64  
65 7 ▶ while ( my ( $f, $m ) = each %trace_fields ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %trace_fields) 21 7 28
66 4 ▶ my $sub = sub {
Subroutine __ANON__: called CC=3 SCAR=11.4
67 4 my $s = shift;
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 21 };
74  
75 ## no critic (TestingAndDebugging::ProhibitNoStrict)
76 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
77 21 *{$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 39 ▶sub throw {
Subroutine throw: called CC=2 SCAR=7.1
Pod documented
84 39 my $proto = shift;
85  
86 39 ▶ $proto->rethrow if ref $proto;
Branch: 1 branch
BranchTrueFalseTotal
if ref $proto 0 39 39
87  
88 39 die $proto->new(@_);
89 }
90  
91 0 ▶sub rethrow {
Subroutine rethrow: not called CC=1 SCAR=6.9
Pod documented
92 0 my $self = shift;
93  
94 0 die $self;
95 }
96  
97 39 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod documented
98 39 my $proto = shift;
99 39 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0390
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 39 my $self = bless {}, $class;
102  
103 39 $self->_initialize(@_);
104  
105 39 return $self;
106 }
107  
108 39 ▶sub _initialize {
Subroutine _initialize: called CC=14 SCAR=26.4
109 39 my $self = shift;
110 39 ▶ my %p = @_ == 1 ? ( error => $_[0] ) : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 4 35 39
111  
112 39 ▶ $self->{message} = $p{message} || $p{error} || q{};
Condition100%5/5
$p{'message'} || $p{'error'} || ''
l!l
327
$p{"message"} or $p{"error"}
l!l&&r!l&&!r
1317
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 39 ▶ $self->{show_trace} = $p{show_trace} if exists $p{show_trace};
Branch: 1 branch
BranchTrueFalseTotal
if exists $p{"show_trace"} 2 37 39
115  
116 39 ▶ if ( $self->NoContextInfo ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->NoContextInfo) { } 1 38 39
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 38 $self->{time} = CORE::time();
124 38 $self->{pid} = $$;
125 38 $self->{uid} = $<;
126 38 $self->{euid} = $>;
127 38 $self->{gid} = $(;
128 38 $self->{egid} = $);
129  
130 38 my @ignore_class = (__PACKAGE__);
131 38 my @ignore_package = 'Exception::Class';
132  
133 38 ▶ if ( my $i = delete $p{ignore_class} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $i = delete $p{"ignore_class"}) 1 37 38
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 38 ▶ if ( my $i = delete $p{ignore_package} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $i = delete $p{"ignore_package"}) 2 36 38
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 $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 38 ▶ map { $p{$_} ? ( $_ => delete $p{$_} ) : () } qw(
Branch: 1 branch
BranchTrueFalseTotal
$p{$_} ? : 2 112 114
148 frame_filter
149 filter_frames_early
150 skip_frames
151 ),
152 );
153 }
154  
155 39 my %fields = map { $_ => 1 } $self->Fields;
156 39 ▶ while ( my ( $key, $value ) = each %p ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %p) 39 39 78
157 39 ▶ next if $key =~ /^(?:error|message|show_trace)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^(?:error|message|show_trace)$/ 34 5 39
158  
159 5 ▶ if ( $fields{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fields{$key}) { } 5 0 5
160 5 $self->{$key} = $value;
161 }
162 else {
163 0 Exception::Class::Base->throw(
164 error => "unknown field $key passed to constructor for class "
165 . ref $self );
166 }
167 }
168 }
169  
170 1 ▶sub context_hash {
Subroutine context_hash: called CC=1 SCAR=0.0
Pod documented
171 1 my $self = shift;
172  
173 return {
174 time => $self->{time},
175 pid => $self->{pid},
176 uid => $self->{uid},
177 euid => $self->{euid},
178 gid => $self->{gid},
179 egid => $self->{egid},
180 1 };
181 }
182  
183 1 ▶sub field_hash {
Subroutine field_hash: called CC=2 SCAR=6.9
Pod documented
184 1 my $self = shift;
185  
186 1 my $hash = {};
187  
188 1 for my $field ( $self->Fields ) {
189 2 $hash->{$field} = $self->$field;
190 }
191  
192 1 return $hash;
193 }
194  
195 2 ▶sub description {
Subroutine description: called CC=1 SCAR=0.0
Pod documented
196 2 return 'Generic exception';
197 }
198  
199 9 ▶sub show_trace {
Subroutine show_trace: called CC=4 SCAR=14.6
Pod documented
200 9 my $self = shift;
201  
202 9 ▶ return 0 unless $self->{trace};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"trace"} 0 9 9
203  
204 9 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 9 9
205 0 $self->{show_trace} = shift;
206 }
207  
208 9 ▶ return exists $self->{show_trace} ? $self->{show_trace} : $self->Trace;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'show_trace'} ? : 2 7 9
209 }
210  
211 9 ▶sub as_string {
Subroutine as_string: called CC=6 SCAR=24.2
Pod documented
212 9 my $self = shift;
213  
214 9 my $str = $self->full_message;
215 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
216 0 my $desc = $self->description;
217 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
218 && length $desc ? "[$desc]" : '[Generic exception]';
219 }
220  
221 9 ▶ $str .= "\n\n" . $self->trace->as_string
Branch: 1 branch
BranchTrueFalseTotal
if $self->show_trace 7 2 9
222 if $self->show_trace;
223  
224 9 return $str;
225 }
226  
227 8 ▶sub full_message { $_[0]->message }
Subroutine full_message: called CC=1 SCAR=0.0
Pod documented
228  
229 #
230 # The %seen bit protects against circular inheritance.
231 #
232 ## no critic (BuiltinFunctions::ProhibitStringyEval, ErrorHandling::RequireCheckingReturnValueOfEval)
233 7 ▶eval <<'EOF' if $] == 5.006;
Branch: 1 branch
BranchTrueFalseTotal
if $] == "5.006" 0 7 7
234 sub isa {
235 my ( $inheritor, $base ) = @_;
236 $inheritor = ref($inheritor) if ref($inheritor);
237  
238 my %seen;
239  
240 no strict 'refs';
241 my @parents = ( $inheritor, @{"$inheritor\::ISA"} );
242 while ( my $class = shift @parents ) {
243 return 1 if $class eq $base;
244  
245 push @parents, grep { !$seen{$_}++ } @{"$class\::ISA"};
246 }
247 return 0;
248 }
249 EOF
250  
251 3 ▶sub caught {
Subroutine caught: called CC=4 SCAR=14.5
Pod undocumented
252 3 my $class = shift;
253  
254 3 my $e = $@;
255  
256 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
257 2 return $e;
258 }
259  
260 7 1;
261  
262 # ABSTRACT: A base class for exception objects
263  
264 __END__
« Class.pm ↑ Summary