blib/lib/Venus/Fault.pm

filter: Statementstmt 100% 48 / 48 Branchbran 83.3% 10 / 12 Conditioncond 100% 9 / 9 MC/DCmcdc 100% 5 / 5 Subroutinesub 100% 12 / 12 Podpod 100% 5 / 5 totaltotal 97.8% 89 / 91 CC 11 SCAR 10.0
CC
11
Cov
97%
trace
5.0
explain
4.0
throw
2.0
CRAP 2.7
« blib/lib/Venus/Role/Printable.pm ↑ Summary blib/lib/Venus/Sealed.pm »
1 package Venus::Fault;
2  
3 11 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # OVERLOADS
9  
10 11 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
11 '""' => 'explain',
12 1 ▶ 'eq' => sub{$_[0]->{message} eq "$_[1]"},
Subroutine __ANON__: called CC=1 SCAR=0.0
13 1 ▶ 'ne' => sub{$_[0]->{message} ne "$_[1]"},
Subroutine __ANON__: called CC=1 SCAR=0.0
14 1 ▶ 'qr' => sub{qr/@{[quotemeta($_[0]->{message})]}/},
Subroutine __ANON__: called CC=1 SCAR=0.0
15 11 '~~' => 'explain',
16 fallback => 1,
17 11 );
18  
19 # METHODS
20  
21 49 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
22 49 ▶ return bless({message => $_[1] || 'Exception!'})->trace;
Condition100%2/2
$_[1] || 'Exception!'
l!l
3217
MC/DC100%1/1
$_[1] || 'Exception!'
$_[1]
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
23 }
24  
25 102 ▶sub explain {
Subroutine explain: called CC=4 SCAR=13.9
Pod documented
26 102 my ($self) = @_;
27  
28 102 ▶ $self->trace(1) if !@{$self->frames};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->frames;} 0 102 102
29  
30 102 my $frames = $self->{frames};
31  
32 102 my $file = $frames->[0][1];
33 102 my $line = $frames->[0][2];
34 102 my $pack = $frames->[0][0];
35 102 my $subr = $frames->[0][3];
36  
37 102 my $message = $self->{message};
38  
39 102 my @stacktrace = ("$message in $file at line $line");
40  
41 102 ▶ push @stacktrace, 'Traceback (reverse chronological order):' if @$frames > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$frames > 1 100 2 102
42  
43 102 @stacktrace = (join("\n\n", grep defined, @stacktrace), '');
44  
45 102 ▶ for (my $i = 1; $i < @$frames; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$frames) 1468 102 1570
46 1468 my $pack = $frames->[$i][0];
47 1468 my $file = $frames->[$i][1];
48 1468 my $line = $frames->[$i][2];
49 1468 my $subr = $frames->[$i][3];
50  
51 1468 push @stacktrace, "$subr\n in $file at line $line";
52 }
53  
54 102 return join "\n", @stacktrace, "";
55 }
56  
57 158 ▶sub frames {
Subroutine frames: called CC=2 SCAR=6.9
Pod documented
58 158 my ($self) = @_;
59  
60 158 ▶ return $self->{frames} //= [];
Condition100%2/2
$self->{"frames"} //= []
l!l
10949
MC/DC100%1/1
$self->{"frames"} //= []
$self->{"frames"}
Truth table100%2/2
A
A= $self->{"frames"}
Aresult
11
00
61 }
62  
63 36 ▶sub throw {
Subroutine throw: called CC=2 SCAR=7.1
Pod documented
64 36 my ($self, @args) = @_;
65  
66 36 ▶ $self = $self->new(@args) if !ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 36 36
67  
68 36 die $self;
69 }
70  
71 52 ▶sub trace {
Subroutine trace: called CC=5 SCAR=16.1
Pod documented
72 52 my ($self, $offset, $limit) = @_;
73  
74 52 my $frames = $self->frames;
75  
76 52 @$frames = ();
77  
78 52 ▶ for (my $i = $offset // 1; my @caller = caller($i); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@caller) = caller $i) 746 50 796
Condition100%2/2
$offset // 1
l!l
250
MC/DC100%1/1
$offset // 1
$offset
Truth table100%2/2
A
A= $offset
Aresult
11
00
79 746 push @$frames, [@caller];
80  
81 746 ▶ last if defined $limit && $i + 1 == $offset + $limit;
Branch: 1 branch
BranchTrueFalseTotal
if defined $limit and $i + 1 == $offset + $limit 2 744 746
Condition100%3/3
defined $limit and $i + 1 == $offset + $limit
!ll&&!rl&&r
74312
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
82 }
83  
84 52 return $self;
85 }
86  
87 11 1;
88  
89  
90  
91 =head1 NAME
92  
93 Venus::Fault - Fault Class
94  
95 =cut
96  
97 =head1 ABSTRACT
98  
99 Fault Class for Perl 5
100  
101 =cut
102  
103 =head1 SYNOPSIS
104  
105 package main;
106  
107 use Venus::Fault;
108  
109 my $fault = Venus::Fault->new;
110  
111 # $fault->throw;
112  
113 =cut
114  
115 =head1 DESCRIPTION
116  
117 This package represents a generic system error (exception object).
118  
119 =cut
120  
121 =head1 METHODS
122  
123 This package provides the following methods:
124  
125 =cut
126  
127 =head2 explain
128  
129 explain() (string)
130  
131 The explain method returns the error message and is used in stringification
132 operations.
133  
134 I<Since C<1.80>>
135  
136 =over 4
137  
138 =item explain example 1
139  
140 # given: synopsis;
141  
142 my $explain = $fault->explain;
143  
144 # "Exception! in ...
145  
146 =back
147  
148 =cut
149  
150 =head2 frames
151  
152 frames() (arrayref)
153  
154 The frames method returns the compiled and stashed stack trace data.
155  
156 I<Since C<1.80>>
157  
158 =over 4
159  
160 =item frames example 1
161  
162 # given: synopsis;
163  
164 my $frames = $fault->frames;
165  
166 # [
167 # ...
168 # [
169 # "main",
170 # "t/Venus_Fault.t",
171 # ...
172 # ],
173 # ]
174  
175 =back
176  
177 =cut
178  
179 =head2 new
180  
181 new(any @args) (Venus::Fault)
182  
183 The new method constructs an instance of the package.
184  
185 I<Since C<4.15>>
186  
187 =over 4
188  
189 =item new example 1
190  
191 package main;
192  
193 use Venus::Fault;
194  
195 my $new = Venus::Fault->new;
196  
197 # bless(..., "Venus::Fault")
198  
199 =back
200  
201 =over 4
202  
203 =item new example 2
204  
205 package main;
206  
207 use Venus::Fault;
208  
209 my $new = Venus::Fault->new('Oops!');
210  
211 # bless(..., "Venus::Fault")
212  
213 =back
214  
215 =cut
216  
217 =head2 throw
218  
219 throw(string $message) (Venus::Fault)
220  
221 The throw method throws an error if the invocant is an object, or creates an
222 error object using the arguments provided and throws the created object.
223  
224 I<Since C<1.80>>
225  
226 =over 4
227  
228 =item throw example 1
229  
230 # given: synopsis;
231  
232 my $throw = $fault->throw;
233  
234 # bless({ ... }, 'Venus::Fault')
235  
236 =back
237  
238 =cut
239  
240 =head2 trace
241  
242 trace(number $offset, number $limit) (Venus::Fault)
243  
244 The trace method compiles a stack trace and returns the object. By default it
245 skips the first frame.
246  
247 I<Since C<1.80>>
248  
249 =over 4
250  
251 =item trace example 1
252  
253 # given: synopsis;
254  
255 my $trace = $fault->trace;
256  
257 # bless({ ... }, 'Venus::Fault')
258  
259 =back
260  
261 =over 4
262  
263 =item trace example 2
264  
265 # given: synopsis;
266  
267 my $trace = $fault->trace(0, 1);
268  
269 # bless({ ... }, 'Venus::Fault')
270  
271 =back
272  
273 =over 4
274  
275 =item trace example 3
276  
277 # given: synopsis;
278  
279 my $trace = $fault->trace(0, 2);
280  
281 # bless({ ... }, 'Venus::Fault')
282  
283 =back
284  
285 =cut
286  
287 =head1 OPERATORS
288  
289 This package overloads the following operators:
290  
291 =cut
292  
293 =over 4
294  
295 =item operation: C<("")>
296  
297 This package overloads the C<""> operator.
298  
299 B<example 1>
300  
301 # given: synopsis;
302  
303 my $result = "$fault";
304  
305 # "Exception!"
306  
307 =back
308  
309 =over 4
310  
311 =item operation: C<(eq)>
312  
313 This package overloads the C<eq> operator.
314  
315 B<example 1>
316  
317 # given: synopsis;
318  
319 my $result = $fault eq 'Exception!';
320  
321 # 1
322  
323 =back
324  
325 =over 4
326  
327 =item operation: C<(ne)>
328  
329 This package overloads the C<ne> operator.
330  
331 B<example 1>
332  
333 # given: synopsis;
334  
335 my $result = $fault ne 'exception!';
336  
337 # 1
338  
339 =back
340  
341 =over 4
342  
343 =item operation: C<(qr)>
344  
345 This package overloads the C<qr> operator.
346  
347 B<example 1>
348  
349 # given: synopsis;
350  
351 my $test = 'Exception!' =~ qr/$fault/;
352  
353 # 1
354  
355 =back
356  
357 =over 4
358  
359 =item operation: C<(~~)>
360  
361 This package overloads the C<~~> operator.
362  
363 B<example 1>
364  
365 # given: synopsis;
366  
367 my $result = $fault ~~ 'Exception!';
368  
369 # 1
370  
371 =back
372  
373 =head1 AUTHORS
374  
375 Awncorp, C<awncorp@cpan.org>
376  
377 =cut
378  
379 =head1 LICENSE
380  
381 Copyright (C) 2022, Awncorp, C<awncorp@cpan.org>.
382  
383 This program is free software, you can redistribute it and/or modify it under
384 the terms of the Apache license version 2.0.
385  
386 =cut
« blib/lib/Venus/Role/Printable.pm ↑ Summary blib/lib/Venus/Sealed.pm »