Venus/Error.pm

filter: Statementstmt 100% 96 / 96 Branchbran 88.8% 16 / 18 Conditioncond 88.8% 8 / 9 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 17 / 17 Podpod 66.6% 4 / 6 totaltotal 96.0% 145 / 151 CC 14 SCAR 10.5
CC
14
Cov
98%
explain
5.0
trace
5.0
build_self
4.0
CRAP 2.9
« Venus/Opts.pm ↑ Summary Venus/String.pm »
1 package Venus::Error;
2  
3 18 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 18 ▶use Venus::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 18 base 'Venus::Kind::Utility';
11  
12 18 with 'Venus::Role::Explainable';
13 18 with 'Venus::Role::Stashable';
14  
15 use overload (
16 '""' => 'explain',
17 1 ▶ '.' => sub{$_[0]->message . "$_[1]"},
Subroutine __ANON__: called CC=1 SCAR=0.0
18 1 ▶ 'eq' => sub{$_[0]->message eq "$_[1]"},
Subroutine __ANON__: called CC=1 SCAR=0.0
19 1 ▶ 'ne' => sub{$_[0]->message ne "$_[1]"},
Subroutine __ANON__: called CC=1 SCAR=0.0
20 1 ▶ 'qr' => sub{qr/@{[quotemeta($_[0]->message)]}/},
Subroutine __ANON__: called CC=1 SCAR=0.0
21 18 '~~' => 'explain',
22 fallback => 1,
23 18 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 # ATTRIBUTES
26  
27 18 attr 'context';
28 18 attr 'message';
29  
30 # BUILDERS
31  
32 sub build_arg {
33 2 ▶ my ($self, $data) = @_;
Subroutine build_arg: called CC=1 SCAR=0.0
Pod undocumented
34  
35 return {
36 2 message => $data,
37 };
38 }
39  
40 sub build_self {
41 62 ▶ my ($self, $data) = @_;
Subroutine build_self: called CC=4 SCAR=13.9
Pod undocumented
42  
43 62 ▶ $self->context('(None)') if !$self->context;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->context 15 47 62
44 62 ▶ $self->message('Exception!') if !$self->message;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->message 34 28 62
45 62 ▶ $self->trace(2) if !@{$self->frames};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->frames;} 62 0 62
46  
47 62 return $self;
48 }
49  
50 # METHODS
51  
52 sub explain {
53 305 ▶ my ($self) = @_;
Subroutine explain: called CC=5 SCAR=16.1
Pod documented
54  
55 305 ▶ $self->trace(1, 1) if !@{$self->{'$frames'}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"\$frames"};} 0 305 305
56  
57 305 my $frames = $self->{'$frames'};
58  
59 305 my $file = $frames->[0][1];
60 305 my $line = $frames->[0][2];
61 305 my $pack = $frames->[0][0];
62 305 my $subr = $frames->[0][3];
63  
64 305 my $message = $self->message;
65  
66 305 my @stacktrace = ("$message in $file at line $line");
67  
68 305 push @stacktrace, 'Type:', ref($self);
69 305 ▶ push @stacktrace, 'Context:', $self->context || '(None)';
Condition50%1/2
$self->context || '(None)'
l!l
3050
MC/DC0%0/1
$self->context || '(None)'
$self->context
Truth table50%1/2
A
A= $self->context
Aresult
11
00
70  
71 18 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 305 require Data::Dumper;
74  
75 305 local $Data::Dumper::Indent = 1;
76 305 local $Data::Dumper::Trailingcomma = 0;
77 305 local $Data::Dumper::Purity = 0;
78 305 local $Data::Dumper::Pad = '';
79 305 local $Data::Dumper::Varname = 'VAR';
80 305 local $Data::Dumper::Useqq = 0;
81 305 local $Data::Dumper::Terse = 1;
82 305 local $Data::Dumper::Freezer = '';
83 305 local $Data::Dumper::Toaster = '';
84 305 local $Data::Dumper::Deepcopy = 1;
85 305 local $Data::Dumper::Quotekeys = 0;
86 305 local $Data::Dumper::Bless = 'bless';
87 305 local $Data::Dumper::Pair = ' => ';
88 305 local $Data::Dumper::Maxdepth = 0;
89 305 local $Data::Dumper::Maxrecurse = 1000;
90 305 local $Data::Dumper::Useperl = 0;
91 305 local $Data::Dumper::Sortkeys = 1;
92 305 local $Data::Dumper::Deparse = 1;
93 305 local $Data::Dumper::Sparseseen = 0;
94  
95 305 my $stashed = Data::Dumper->Dump([$self->stash]);
96  
97 305 $stashed =~ s/^'|'$//g;
98  
99 305 chomp $stashed;
100  
101 305 push @stacktrace, 'Stashed:', $stashed;
102 305 ▶ push @stacktrace, 'Traceback (reverse chronological order):' if @$frames > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$frames > 1 303 2 305
103  
104 18 ▶ use warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
105  
106 305 @stacktrace = (join("\n\n", grep defined, @stacktrace), '');
107  
108 305 ▶ for (my $i = 1; $i < @$frames; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$frames) 5632 305 5937
109 5632 my $pack = $frames->[$i][0];
110 5632 my $file = $frames->[$i][1];
111 5632 my $line = $frames->[$i][2];
112 5632 my $subr = $frames->[$i][3];
113  
114 5632 push @stacktrace, "$subr\n in $file at line $line";
115 }
116  
117 305 return join "\n", @stacktrace, "";
118 }
119  
120 sub frames {
121 131 ▶ my ($self) = @_;
Subroutine frames: called CC=2 SCAR=6.9
Pod documented
122  
123 131 ▶ return $self->{'$frames'} //= [];
Condition100%2/2
$self->{"\$frames"} //= []
l!l
6962
MC/DC100%1/1
$self->{"\$frames"} //= []
$self->{"\$frames"}
Truth table100%2/2
A
A= $self->{"\$frames"}
Aresult
11
00
124 }
125  
126 sub throw {
127 50 ▶ my ($self, @args) = @_;
Subroutine throw: called CC=2 SCAR=6.9
Pod documented
128  
129 50 ▶ $self = $self->new(@args) if !ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 2 48 50
130  
131 50 die $self;
132 }
133  
134 sub trace {
135 65 ▶ my ($self, $offset, $limit) = @_;
Subroutine trace: called CC=5 SCAR=16.1
Pod documented
136  
137 65 my $frames = $self->frames;
138  
139 65 @$frames = ();
140  
141 65 ▶ for (my $i = $offset // 1; my @caller = caller($i); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@caller) = caller $i) 1199 63 1262
Condition100%2/2
$offset // 1
l!l
641
MC/DC100%1/1
$offset // 1
$offset
Truth table100%2/2
A
A= $offset
Aresult
11
00
142 1199 push @$frames, [@caller];
143  
144 1199 ▶ last if defined $limit && $i + 1 == $offset + $limit;
Branch: 1 branch
BranchTrueFalseTotal
if defined $limit and $i + 1 == $offset + $limit 2 1197 1199
Condition100%3/3
defined $limit and $i + 1 == $offset + $limit
!ll&&!rl&&r
119612
MC/DC100%2/2
defined $limit and $i + 1 == $offset + $limit
defined $limit $i + 1 == $offset + $limit
Truth table100%3/3
A and B
A= defined $limitB= $i + 1 == $offset + $limit
ABresult
0X0
100
111
145 }
146  
147 65 return $self;
148 }
149  
150 18 1;
151  
152  
153  
154 =head1 NAME
155  
156 Venus::Error - Error Class
157  
158 =cut
159  
160 =head1 ABSTRACT
161  
162 Error Class for Perl 5
163  
164 =cut
165  
166 =head1 SYNOPSIS
167  
168 package main;
169  
170 use Venus::Error;
171  
172 my $error = Venus::Error->new;
173  
174 # $error->throw;
175  
176 =cut
177  
178 =head1 DESCRIPTION
179  
180 This package represents a context-aware error (exception object).
181  
182 =cut
183  
184 =head1 ATTRIBUTES
185  
186 This package has the following attributes:
187  
188 =cut
189  
190 =head2 context
191  
192 context(Str)
193  
194 This attribute is read-write, accepts C<(Str)> values, is optional, and defaults to C<'(None)'>.
195  
196 =cut
197  
198 =head2 message
199  
200 message(Str)
201  
202 This attribute is read-write, accepts C<(Str)> values, is optional, and defaults to C<'Exception!'>.
203  
204 =cut
205  
206 =head1 INHERITS
207  
208 This package inherits behaviors from:
209  
210 L<Venus::Kind::Utility>
211  
212 =cut
213  
214 =head1 INTEGRATES
215  
216 This package integrates behaviors from:
217  
218 L<Venus::Role::Explainable>
219  
220 L<Venus::Role::Stashable>
221  
222 =cut
223  
224 =head1 METHODS
225  
226 This package provides the following methods:
227  
228 =cut
229  
230 =head2 explain
231  
232 explain() (Str)
233  
234 The explain method returns the error message and is used in stringification
235 operations.
236  
237 I<Since C<0.01>>
238  
239 =over 4
240  
241 =item explain example 1
242  
243 # given: synopsis;
244  
245 my $explain = $error->explain;
246  
247 # "Exception! in ...
248  
249 =back
250  
251 =cut
252  
253 =head2 frames
254  
255 frames() (ArrayRef)
256  
257 The frames method returns the compiled and stashed stack trace data.
258  
259 I<Since C<0.01>>
260  
261 =over 4
262  
263 =item frames example 1
264  
265 # given: synopsis;
266  
267 my $frames = $error->frames;
268  
269 # [
270 # ...
271 # [
272 # "main",
273 # "t/Venus_Error.t",
274 # ...
275 # ],
276 # ]
277  
278 =back
279  
280 =cut
281  
282 =head2 throw
283  
284 throw(Any @data) (Error)
285  
286 The throw method throws an error if the invocant is an object, or creates an
287 error object using the arguments provided and throws the created object.
288  
289 I<Since C<0.01>>
290  
291 =over 4
292  
293 =item throw example 1
294  
295 # given: synopsis;
296  
297 my $throw = $error->throw;
298  
299 # bless({ ... }, 'Venus::Error')
300  
301 =back
302  
303 =cut
304  
305 =head2 trace
306  
307 trace(Int $offset, Int $limit) (Error)
308  
309 The trace method compiles a stack trace and returns the object. By default it
310 skips the first frame.
311  
312 I<Since C<0.01>>
313  
314 =over 4
315  
316 =item trace example 1
317  
318 # given: synopsis;
319  
320 my $trace = $error->trace;
321  
322 # bless({ ... }, 'Venus::Error')
323  
324 =back
325  
326 =over 4
327  
328 =item trace example 2
329  
330 # given: synopsis;
331  
332 my $trace = $error->trace(0, 1);
333  
334 # bless({ ... }, 'Venus::Error')
335  
336 =back
337  
338 =over 4
339  
340 =item trace example 3
341  
342 # given: synopsis;
343  
344 my $trace = $error->trace(0, 2);
345  
346 # bless({ ... }, 'Venus::Error')
347  
348 =back
349  
350 =cut
351  
352 =head1 OPERATORS
353  
354 This package overloads the following operators:
355  
356 =cut
357  
358 =over 4
359  
360 =item operation: C<(.)>
361  
362 This package overloads the C<.> operator.
363  
364 B<example 1>
365  
366 # given: synopsis;
367  
368 my $string = $error . ' Unknown';
369  
370 # "Exception! Unknown"
371  
372 =back
373  
374 =over 4
375  
376 =item operation: C<(eq)>
377  
378 This package overloads the C<eq> operator.
379  
380 B<example 1>
381  
382 # given: synopsis;
383  
384 my $result = $error eq 'Exception!';
385  
386 # 1
387  
388 =back
389  
390 =over 4
391  
392 =item operation: C<(ne)>
393  
394 This package overloads the C<ne> operator.
395  
396 B<example 1>
397  
398 # given: synopsis;
399  
400 my $result = $error ne 'exception!';
401  
402 # 1
403  
404 =back
405  
406 =over 4
407  
408 =item operation: C<(qr)>
409  
410 This package overloads the C<qr> operator.
411  
412 B<example 1>
413  
414 # given: synopsis;
415  
416 my $test = 'Exception!' =~ qr/$error/;
417  
418 # 1
419  
420 =back
421  
422 =over 4
423  
424 =item operation: C<("")>
425  
426 This package overloads the C<""> operator.
427  
428 B<example 1>
429  
430 # given: synopsis;
431  
432 my $result = "$error";
433  
434 # "Exception!"
435  
436 =back
437  
438 =over 4
439  
440 =item operation: C<(~~)>
441  
442 This package overloads the C<~~> operator.
443  
444 B<example 1>
445  
446 # given: synopsis;
447  
448 my $result = $error ~~ 'Exception!';
449  
450 # 1
451  
452 =back
« Venus/Opts.pm ↑ Summary Venus/String.pm »