Handle/Exception.pm

filter: Statementstmt 96.6% 87 / 90 Branchbran 65.0% 13 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 92.0% 116 / 126 CC 11 SCAR 9.6
CC
11
Cov
91%
throw
5.2
_gen_tree
3.0
_color_for_line
3.0
CRAP 2.6
↑ Summary Handle.pm »
1 6 ▶use 5.008; # _use_carp_version
Subroutine BEGIN: called CC=1 SCAR=0.0
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Data::Handle::Exception;
6  
7 6 our $VERSION = '1.000001';
8  
9 # ABSTRACT: Super-light Weight Dependency Free Exception base.
10  
11 6 our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42 6 ▶use overload '""' => \&stringify;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 6 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
44 6 ▶use Carp 1.22;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 6 ▶use Term::ANSIColor qw( YELLOW GREEN RESET );
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 6 ▶if ( not defined &Carp::caller_info ) { ## no critic (Subroutines)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &Carp::caller_info) 0 6 6
48 0 Carp::croak(<<"EOF");
49 Cannot load Data::Handle::Exception as your version of Carp does not have
50 ::caller_info which we use for backtraces.
51 Carp Version: $Carp::VERSION
52 EOF
53 }
54  
55  
56  
57  
58  
59  
60  
61  
62 25 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
63 25 my ($class) = @_;
64 25 my $self = {};
65 25 bless $self, $class;
66 25 return $self;
67 }
68  
69  
70  
71  
72  
73  
74  
75 25 ▶sub throw {
Subroutine throw: called CC=5 SCAR=16.5
Pod documented
76 25 my $self = shift;
77  
78 25 ▶ if ( not blessed $self ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($self)) 25 0 25
79 25 $self = $self->new();
80 }
81 25 my $message = shift;
82  
83 25 my @stack = ();
84 25 my @stacklines = ();
85  
86 # This is mostly because want to benefit from all new fixes in carp.
87 25 my $callerinfo = \&Carp::caller_info; ## no critic (Subroutines)
88  
89 { # stolen parts from Carp::ret_backtrace
90 25 my ($i) = 0;
91  
92 25 my $tid_msg = q{};
93 25 ▶ if ( defined &threads::tid ) { ## no critic (Subroutines)
Branch: 1 branch
BranchTrueFalseTotal
if (defined &threads::tid) 0 25 25
94  
95 0 my $tid = threads->tid;
96 0 ▶ $tid_msg = " thread $tid" if $tid;
Branch: 1 branch
BranchTrueFalseTotal
if $tid 0 0 0
97 }
98  
99 25 my %i = $callerinfo->($i);
100  
101 25 push @stack, \%i;
102 25 push @stacklines, sprintf q{Exception '%s' thrown at %s line %s%s}, blessed($self), $i{file}, $i{line}, $tid_msg;
103  
104 25 ▶ while ( my %j = $callerinfo->( ++$i ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(%j) = &$callerinfo(++$i)) 156 25 181
105 156 push @stack, \%j;
106 156 push @stacklines, sprintf q{%s called at %s line %s%s}, $j{sub_name}, $j{file}, $j{line}, $tid_msg;
107 }
108 }
109 25 $self->{message} = $message;
110 25 $self->{stacklines} = \@stacklines;
111 25 $self->{stack} = \@stack;
112 25 Carp::confess($self);
113 }
114  
115 {
116 ## no critic ( RequireInterpolationOfMetachars )
117 6 my $s = q{(\x2F|\x5c)};
118 6 my $d = q{\x2E};
119 ## use critic
120 6 my $yellow = qr{
121 ${s}Try${s}Tiny${d}pm
122 |
123 ${s}Test${s}Fatal${d}pm
124 }x;
125 6 my $green = qr{
126 ${s}Data${s}Handle${d}pm
127 |
128 ${s}Data${s}Handle${s}
129 }x;
130  
131 188 ▶ sub _color_for_line {
Subroutine _color_for_line: called CC=3 SCAR=11.0
132 188 my $line = shift;
133 188 ▶ return YELLOW if ( $line =~ $yellow );
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /$yellow/ 104 84 188
134 84 ▶ return GREEN if ( $line =~ $green );
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /$green/ 32 52 84
135 52 return q{};
136 }
137 }
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172 6 ▶sub stringify {
Subroutine stringify: called CC=2 SCAR=6.9
Pod documented
173 ## no critic ( ProhibitPunctuationVars )
174 26 local $@ = undef; # Term::ANSIColour clobbers $@
175 26 my $self = shift;
176 26 my $message = $self->{message};
177 26 my @stacklines = @{ $self->{stacklines} };
178  
179 26 my $out = $message . "\n\n";
180 26 my $throwline = shift @stacklines;
181 26 $out .= _color_for_line($throwline) . $throwline . RESET;
182 26 my $i = 2;
183 26 for (@stacklines) {
184 162 $out .= "\n " . _color_for_line($_) . "$i. " . $_ . RESET;
185 162 $i++;
186 }
187 26 return $out . "\n\n";
188 }
189 6 my $dynaexceptions = { 'Data::Handle::Exception' => 1 };
190  
191 54 ▶sub _gen {
Subroutine _gen: called CC=2 SCAR=7.0
192 54 my ( undef, $fullclass, $parent ) = @_;
193 ## no critic ( RequireInterpolationOfMetachars )
194 54 my $code = sprintf q{package %s; our @ISA=("%s"); 1;}, $fullclass, $parent;
195  
196 ## no critic ( ProhibitStringyEval RequireCarping ProhibitPunctuationVars )
197 54 ▶ eval $code or throw(qq{ Exception generating exception :[ $@ });
Branch: 1 branch
BranchTrueFalseTotal
eval $code or throw(" Exception generating exception :[ $@ ") 54 0 54
198 54 $dynaexceptions->{$fullclass} = 1;
199 54 return 1;
200 }
201  
202 54 ▶sub _gen_tree {
Subroutine _gen_tree: called CC=3 SCAR=11.0
203 54 my ( $self, $class ) = @_;
204 54 my $parent = $class;
205  
206 54 $parent =~ s{
207 ::[^:]+$
208 }{}x;
209 54 ▶ if ( !exists $dynaexceptions->{$parent} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $dynaexceptions->{$parent}) 12 42 54
210 12 $self->_gen_tree($parent);
211 }
212 54 ▶ if ( !exists $dynaexceptions->{$class} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $dynaexceptions->{$class}) 54 0 54
213 54 $self->_gen( $class, $parent );
214 }
215 54 return $class;
216 }
217  
218 6 for (qw( API::Invalid API::Invalid::Whence API::Invalid::Params API::NotImplemented Internal::BadGet NoSymbol BadFilePos )) {
219 42 __PACKAGE__->_gen_tree("Data::Handle::Exception::$_");
220 }
221  
222 6 1;
223  
224 __END__
↑ Summary Handle.pm »