Tiny/Base.pm

filter: Statementstmt 95.8% 70 / 73 Branchbran 70.0% 14 / 20 Conditioncond 55.5% 5 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 93.3% 14 / 15 Podpod 40.0% 2 / 5 totaltotal 82.0% 105 / 128 CC 14 SCAR 11.2
CC
14
Cov
87%
_get_call_stack
5.1
__spew
5.0
_get_strval
3.8
CRAP 3.1
↑ Summary Tiny.pm »
1 package X::Tiny::Base;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 my %CALL_STACK;
7  
8 my %PROPAGATIONS;
9  
10 =encoding utf-8
11  
12 =head1 NAME
13  
14 X::Tiny::Base - super-light exception base class
15  
16 =head1 SYNOPSIS
17  
18 package My::Module::X::Base;
19  
20 use parent qw( X::Tiny::Base );
21  
22 sub _new {
23 my ($class, @args) = @_;
24  
25 ...
26 }
27  
28 #Optionally, redefine this:
29 sub get {
30 my ($self, $attr_name) = @_;
31  
32 ...
33 }
34  
35 #Optionally, redefine this:
36 sub get_message { ... }
37  
38 #Optionally, redefine this:
39 sub to_string { ... }
40  
41 #If you override this, be sure also to call the base method.
42 sub DESTROY {
43 my ($self) = @_;
44  
45 ...
46  
47 #vv This. Be sure to do this in your override method.
48 $self->SUPER::DESTROY();
49 }
50  
51 =head1 DESCRIPTION
52  
53 This base class can be subclassed into your distribution’s own
54 exception base class (e.g., C<My::Module::X::Base>), or you can treat it
55 as that base class itself (i.e., forgo C<My::Module::X::Base>).
56  
57 C<X::Tiny::Base> serves two functions:
58  
59 =over
60  
61 =item 1) It is a useful set of defaults for overridable methods.
62  
63 =item 2) Framework handling of L<overload> stringification behavior,
64 e.g., when an uncaught exception is printed.
65  
66 =back
67  
68 That stringification’s precise formatting is not defined; however, it
69 will always include, in addition to the exception’s main message:
70  
71 =over
72  
73 =item * A stack trace (including function arguments)
74  
75 B<IMPORTANT:> For security purposes, take care not to expose any function
76 arguments that might contain sensitive information (e.g., passwords).
77  
78 Note that, in pre-5.16 Perls, this writes to the C<@DB::args> global.
79 (That shouldn’t affect you, but it’s interaction with the environment, so
80 better documented than not.)
81  
82 =item * Propagations
83  
84 =back
85  
86 There is currently no access provided in code to these; if that’s something
87 you’d like to have, let me know.
88  
89 B<NOTE:> The overload stringification doesn’t seem to work as implemented in
90 Perl 5.8 or earlier. Perl 5.8 went end-of-life on 14 December 2008. Yeah.
91  
92 =head1 SUBCLASS INTERFACE
93  
94 The default behaviors seem pretty usable and desirable to me, but there may
95 be circumstances where someone wants other behaviors. Toward that end,
96 the following methods are meant to be overridden in subclasses:
97  
98 =head2 I<CLASS>->OVERLOAD()
99  
100 Returns a boolean to indicate whether this exception class should load
101 L<overload> as part of creating exceptions. If you don’t want the
102 memory overhead of L<overload>, then make this return 0. It returns 1
103 by default.
104  
105 You might also make this 0 if, for example, you want to handle the
106 L<overload> behavior yourself. (But at that point, why use X::Tiny??)
107  
108 =cut
109  
110 2 ▶use constant OVERLOAD => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
111  
112 =head2 I<CLASS>->_new( MESSAGE, KEY1 => VALUE1, .. )
113  
114 The main constructor. Whatever args this accepts are the args that
115 you should use to create exceptions via your L<X::Tiny> subclass’s
116 C<create()> method. You’re free to design whatever internal representation
117 you want for your class: hash reference, array reference, etc.
118  
119 The default implementation accepts a string message and, optionally, a
120 list of key/value pairs. It is useful that subclasses of your base class
121 define their own MESSAGE, so all you’ll pass in is a specific piece of
122 information about this instance—e.g., an error code, a parameter name, etc.
123  
124 =cut
125  
126 sub _new {
127 5 ▶ my ( $class, $string, %attrs ) = @_;
Subroutine _new: called CC=1 SCAR=0.0
128  
129 5 return bless [ $string, \%attrs ], $class;
130 }
131  
132 =head2 I<OBJ>->get_messaage()
133  
134 Return the exception’s main MESSAGE.
135 This is useful for contexts where you want to encapsulate the error
136 internals from how you’re reporting them, e.g., for protocols.
137  
138 =cut
139  
140 sub get_message {
141 1 ▶ return $_[0][0];
Subroutine get_message: called CC=1 SCAR=0.0
Pod undocumented
142 }
143  
144 =head2 I<OBJ>->get( ATTRIBUTE_NAME )
145  
146 Retrieves the value of an attribute.
147  
148 =cut
149  
150 sub get {
151 0 ▶ my ( $self, $attr ) = @_;
Subroutine get: not called CC=1 SCAR=6.9
Pod documented
152  
153 #Do we need to clone this? Could JSON suffice, or do we need Clone?
154 0 return $self->[1]{$attr};
155 }
156  
157 =head2 I<OBJ>->to_string()
158  
159 Creates a simple string representation of your exception. The default
160 implementation contains the class and the MESSAGE given on instantiation.
161  
162 This method’s return value should B<NOT> include a strack trace;
163 L<X::Tiny::Base>’s internals handle that one for you.
164  
165 =cut
166  
167 sub to_string {
168 5 ▶ my ($self) = @_;
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
169  
170 5 return sprintf '%s: %s', ref($self), $self->[0];
171 }
172  
173 #----------------------------------------------------------------------
174  
175 =head1 DESTRUCTOR METHODS
176  
177 If you define your own C<DESTROY()> method, make sure you also call
178 C<SUPER::DESTROY()>, or else you’ll get memory leaks as L<X::Tiny::Base>’s
179 internal tracking of object properties will never be cleared out.
180  
181 =cut
182  
183 sub DESTROY {
184 5 ▶ my ($self) = @_;
Subroutine DESTROY: called CC=1 SCAR=0.0
185  
186 5 delete $CALL_STACK{$self->_get_strval()};
187 5 delete $PROPAGATIONS{$self->_get_strval()};
188  
189 5 return;
190 }
191  
192 #----------------------------------------------------------------------
193  
194 sub new {
195 5 ▶ my ($class, @args) = @_;
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
196  
197 5 ▶ $class->_check_overload() if $class->OVERLOAD();
Branch: 1 branch
BranchTrueFalseTotal
if $class->OVERLOAD 5 0 5
198  
199 5 my $self = $class->_new(@args);
200  
201 5 $CALL_STACK{$self->_get_strval()} = [ _get_call_stack(2) ];
202  
203 5 return $self;
204 }
205  
206 #----------------------------------------------------------------------
207  
208 sub PROPAGATE {
209 1 ▶ my ($self, $file, $line) = @_;
Subroutine PROPAGATE: called CC=1 SCAR=0.0
Pod undocumented
210  
211 1 push @{ $PROPAGATIONS{$self->_get_strval()} }, [ $file, $line ];
212  
213 1 return $self;
214 }
215  
216 2 my %_OVERLOADED;
217  
218 sub _check_overload {
219 5 ▶ my ( $class, $str ) = @_;
Subroutine _check_overload: called CC=3 SCAR=11.1
220  
221 #cf. eval_bug.readme
222 5 my $eval_err = $@;
223  
224 2 ▶ $_OVERLOADED{$class} ||= eval qq{
Subroutine BEGIN: called CC=1 SCAR=0.0
Condition66%2/3
$_OVERLOADED{$class} ||= eval qq[\n package $class;\n use overload (q<""> => __PACKAGE__->can('__spew'));\n 1;\n ]
l!l&&r!l&&!r
320
MC/DC0%0/2
$_OVERLOADED{$class} ||= eval qq[\n package $class;\n use overload (q<""> => __PACKAGE__->can('__spew'));\n 1;\n ]
$_OVERLOADED{$class} eval qq[\n package $class;\n use overload (q<""> => __PACKAGE__->can('__spew'));\n 1;\n ]
Truth table66%2/3
A ||= B
A= $_OVERLOADED{$class}B= eval qq[\n package $class;\n use overload (q<""> => __PACKAGE__->can('__spew'));\n 1;\n ]
ABresult
1X1
011
000
225 package $class;
226 use overload (q<""> => __PACKAGE__->can('__spew'));
227 1;
228 };
229  
230 #Should never happen as long as overload.pm is available.
231 5 ▶ warn if !$_OVERLOADED{$class};
Branch: 1 branch
BranchTrueFalseTotal
unless $_OVERLOADED{$class} 0 5 5
232  
233 5 $@ = $eval_err;
234  
235 5 return;
236 }
237  
238 sub _get_strval {
239 27 ▶ my ($self) = @_;
Subroutine _get_strval: called CC=3 SCAR=13.3
240  
241 27 ▶ if ( overload->can('Overloaded') && overload::Overloaded($self) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("overload"->can("Overloaded") and overload::Overloaded($self)) 27 0 27
Condition33%1/3
"overload"->can("Overloaded") and overload::Overloaded($self)
!ll&&!rl&&r
0027
MC/DC0%0/2
"overload"->can("Overloaded") and overload::Overloaded($self)
"overload"->can("Overloaded") overload::Overloaded($self)
Truth table33%1/3
A and B
A= "overload"->can("Overloaded")B= overload::Overloaded($self)
ABresult
0X0
100
111
242 27 return overload::StrVal($self);
243 }
244  
245 0 return q<> . $self;
246 }
247  
248 sub _get_call_stack {
249 5 ▶ my ($level) = @_;
Subroutine _get_call_stack: called CC=5 SCAR=16.3
250  
251 5 my @stack;
252  
253 package DB;
254  
255 #This local() causes pre-5.16 Perl to segfault.
256 5 ▶ local @DB::args if $^V ge v5.16.0;
Branch: 1 branch
BranchTrueFalseTotal
if $^V ge v5.16.0 5 0 5
257  
258 5 ▶ while ( my @call = (caller $level)[3, 1, 2] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@call) = (caller $level)[3, 1, 2]) 12 5 17
259 12 my ($pkg) = ($call[0] =~ m<(.+)::>);
260  
261 12 ▶ if (!$pkg || !$pkg->isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pkg and $pkg->isa(__PACKAGE__)) 12 0 12
Condition66%2/3
$pkg and $pkg->isa(__PACKAGE__)
!ll&&!rl&&r
2100
MC/DC0%0/2
$pkg and $pkg->isa(__PACKAGE__)
$pkg $pkg->isa(__PACKAGE__)
Truth table66%2/3
A and B
A= $pkgB= $pkg->isa(__PACKAGE__)
ABresult
0X0
100
111
262 12 push @call, [ @DB::args ]; #need to copy the array
263 12 push @stack, \@call;
264 }
265  
266 12 $level++;
267 }
268  
269 5 return @stack;
270 }
271  
272 sub __spew {
273 5 ▶ my ($self) = @_;
Subroutine __spew: called CC=5 SCAR=16.1
274  
275 5 my $spew = $self->to_string();
276  
277 5 ▶ if ( rindex($spew, $/) != (length($spew) - length($/)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (rindex($spew, $/) != length($spew) - length($/)) 5 0 5
278 5 my ($args, @printable);
279 $spew .= $/ . join( q<>, map {
280  
281 #Oof. In order to avoid warn()ing on undefined values
282 #(and to distinguish '' from undef) we now quote scalars.
283 @printable = map {
284 31 ▶ ref() ? $_ : !defined() ? 'undef' : do {
Branch: 2 branches
BranchTrueFalseTotal
ref $_ ? : 2 29 31
!defined($_) ? : 1 28 29
285 28 s<'><\\'>g;
286 28 "'$_'"
287 }
288 15 } @{ $_->[3] };
289  
290 15 $args = join(', ', @printable );
291 15 "\t==> $_->[0]($args) (called in $_->[1] at line $_->[2])$/"
292 5 } @{ $CALL_STACK{$self->_get_strval()} } );
293 }
294  
295 5 ▶ if ( $PROPAGATIONS{ $self->_get_strval() } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PROPAGATIONS{$self->_get_strval}) 1 4 5
296 1 $spew .= join( q<>, map { "\t...propagated at $_->[0], line $_->[1]$/" } @{ $PROPAGATIONS{$self->_get_strval()} } );
297 }
298  
299 5 return $spew;
300 }
301  
302 2 1;
↑ Summary Tiny.pm »