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