blib/lib/Venus/Role/Throwable.pm

filter: Statementstmt 89.2% 50 / 56 Branchbran 81.2% 26 / 32 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 7 / 7 Podpod 66.6% 2 / 3 totaltotal 85.4% 88 / 103 SCAR 30.0
CC
19
Cov
86%
throw
20.0
error
2.0
BEGIN
1.0
CRAP 20.1
« blib/lib/Venus/Opts.pm ↑ Summary blib/lib/Venus/Role/Printable.pm »
1 package Venus::Role::Throwable;
2  
3 96 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 96 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 96 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 96 ▶use Venus::Role 'with';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # METHODS
11  
12 sub error {
13 253 ▶ my ($self, $data) = @_;
Subroutine error: called CC=2 SCAR=6.9
Pod documented
14  
15 253 my @args = $data;
16  
17 253 ▶ unshift @args, delete $data->{throw} if $data->{throw};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"throw"} 250 3 253
18  
19 253 @_ = ($self, @args);
20  
21 253 goto $self->can('throw');
22 }
23  
24 sub throw {
25 263 ▶ my ($self, $data, @args) = @_;
Subroutine throw: called CC=18 SCAR=30.0
Pod documented
26  
27 263 require Venus::Throw;
28  
29 263 my $throw = Venus::Throw->new(context => (caller(1))[3])->do(
30 frame => 1,
31 );
32  
33 263 ▶ if (!$data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 5 258 263
34 5 return $throw->do(
35 'package', join('::', map ucfirst, ref($self), 'error')
36 );
37 }
38 258 ▶ if (ref $data ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data ne "HASH") 255 3 258
39 255 ▶ if ($data =~ /^\w+$/ && $self->can($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /^\w+$/u and $self->can($data)) { } 254 1 255
Condition66%2/3
$data =~ /^\w+$/u and $self->can($data)
!ll&&!rl&&r
10254
MC/DC50%1/2
$data =~ /^\w+$/u and $self->can($data)
$data =~ /^\w+$/u $self->can($data)
Truth table66%2/3
A and B
A= $data =~ /^\w+$/uB= $self->can($data)
ABresult
0X0
100
111
40 254 $data = $self->$data(@args);
41 }
42 else {
43 1 return $throw->do(
44 'package', $data,
45 );
46 }
47 }
48  
49 257 ▶ if (exists $data->{as}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"as"}) 0 257 257
50 0 $throw->as($data->{as});
51 }
52 257 ▶ if (exists $data->{capture}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"capture"}) 6 251 257
53 6 $throw->capture(@{$data->{capture}});
54 }
55 257 ▶ if (exists $data->{context}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"context"}) 0 257 257
56 0 $throw->context($data->{context});
57 }
58 257 ▶ if (exists $data->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"error"}) 0 257 257
59 0 $throw->error($data->{error});
60 }
61 257 ▶ if (exists $data->{frame}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"frame"}) 0 257 257
62 0 $throw->frame($data->{frame});
63 }
64 257 ▶ if (exists $data->{message}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"message"}) 250 7 257
65 250 $throw->message($data->{message});
66 }
67 257 ▶ if (exists $data->{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"name"}) 256 1 257
68 256 $throw->name($data->{name});
69 }
70 257 ▶ if (exists $data->{package}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{'package'}) { } 1 256 257
71 1 $throw->package($data->{package});
72 }
73 else {
74 256 $throw->package(join('::', map ucfirst, ref($self), 'error'));
75 }
76 257 ▶ if (exists $data->{parent}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"parent"}) 0 257 257
77 0 $throw->parent($data->{parent});
78 }
79 257 ▶ if (exists $data->{stash}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"stash"}) 250 7 257
80 250 $throw->stash($_, $data->{stash}->{$_}) for keys %{$data->{stash}};
81 }
82 257 ▶ if (exists $data->{on}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"on"}) 0 257 257
83 0 $throw->on($data->{on});
84 }
85 257 ▶ if (exists $data->{raise}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"raise"}) 252 5 257
86 252 @_ = ($throw);
87 252 goto $throw->can('error');
88 }
89  
90 5 return $throw;
91 }
92  
93 # EXPORTS
94  
95 sub EXPORT {
96 104 ▶ ['error', 'throw']
Subroutine EXPORT: called CC=1 SCAR=0.0
Pod undocumented
97 }
98  
99 96 1;
100  
101  
102  
103 =head1 NAME
104  
105 Venus::Role::Throwable - Throwable Role
106  
107 =cut
108  
109 =head1 ABSTRACT
110  
111 Throwable Role for Perl 5
112  
113 =cut
114  
115 =head1 SYNOPSIS
116  
117 package Example;
118  
119 use Venus::Class;
120  
121 with 'Venus::Role::Throwable';
122  
123 package main;
124  
125 my $example = Example->new;
126  
127 # $example->throw;
128  
129 =cut
130  
131 =head1 DESCRIPTION
132  
133 This package modifies the consuming package and provides a mechanism for
134 throwing context-aware errors (exceptions).
135  
136 =cut
137  
138 =head1 METHODS
139  
140 This package provides the following methods:
141  
142 =cut
143  
144 =head2 error
145  
146 error(hashref $data) (any)
147  
148 The error method dispatches to the L</throw> method, excepts a hashref of
149 options to be provided to the L</throw> method, and returns the result unless
150 an exception is raised automatically. If the C<throw> option is provided it is
151 excepted to be the name of a method used as a callback to provide arguments to
152 the thrower.
153  
154 I<Since C<3.40>>
155  
156 =over 4
157  
158 =item error example 1
159  
160 package main;
161  
162 my $example = Example->new;
163  
164 my $throw = $example->error;
165  
166 # bless({ "package" => "Example::Error", ..., }, "Venus::Throw")
167  
168 # $throw->error;
169  
170 =back
171  
172 =over 4
173  
174 =item error example 2
175  
176 package main;
177  
178 my $example = Example->new;
179  
180 my $throw = $example->error({package => 'Example::Error::Unknown'});
181  
182 # bless({ "package" => "Example::Error::Unknown", ..., }, "Venus::Throw")
183  
184 # $throw->error;
185  
186 =back
187  
188 =over 4
189  
190 =item error example 3
191  
192 package main;
193  
194 my $example = Example->new;
195  
196 my $throw = $example->error({
197 name => 'on.example',
198 capture => [$example],
199 stash => {
200 time => time,
201 },
202 });
203  
204 # bless({ "package" => "Example::Error", ..., }, "Venus::Throw")
205  
206 # $throw->error;
207  
208 =back
209  
210 =over 4
211  
212 =item error example 4
213  
214 # given: synopsis
215  
216 package Example;
217  
218 # ...
219  
220 sub error_on_example {
221 my ($self) = @_;
222  
223 return {
224 name => 'on.example',
225 capture => [$example],
226 stash => {
227 time => time,
228 },
229 };
230 }
231  
232 package main;
233  
234 my $throw = $example->error({throw => 'error_on_example'});
235  
236 # bless({ "package" => "Example::Error", ..., }, "Venus::Throw")
237  
238 # $throw->error;
239  
240 =back
241  
242 =over 4
243  
244 =item error example 5
245  
246 # given: synopsis
247  
248 package Example;
249  
250 # ...
251  
252 sub error_on_example {
253 my ($self) = @_;
254  
255 return {
256 name => 'on.example',
257 capture => [$example],
258 stash => {
259 time => time,
260 },
261 raise => 1,
262 };
263 }
264  
265 package main;
266  
267 my $throw = $example->error({throw => 'error_on_example'});
268  
269 # Exception! (isa Example::Error)
270  
271 =back
272  
273 =cut
274  
275 =head2 throw
276  
277 throw(maybe[string | hashref] $data, any @args) (any)
278  
279 The throw method builds a L<Venus::Throw> object, which can raise errors
280 (exceptions). If passed a string representing a package name, the throw object
281 will be configured to throw an exception using that package name. If passed a
282 string representing a method name, the throw object will call that method
283 expecting a hashref to be returned which will be provided to L<Venus::Throw> as
284 arguments to configure the thrower. If passed a hashref, the keys and values
285 are expected to be method names and arguments which will be called to configure
286 the L<Venus::Throw> object returned. If passed additional arguments, assuming
287 they are preceeded by a string representing a method name, the additional
288 arguments will be supplied to the method when called. If the C<raise> argument
289 is provided (or returned from the callback), the thrower will automatically
290 throw the exception.
291  
292 I<Since C<0.01>>
293  
294 =over 4
295  
296 =item throw example 1
297  
298 package main;
299  
300 my $example = Example->new;
301  
302 my $throw = $example->throw;
303  
304 # bless({ "package" => "Example::Error", ..., }, "Venus::Throw")
305  
306 # $throw->error;
307  
308 =back
309  
310 =over 4
311  
312 =item throw example 2
313  
314 package main;
315  
316 my $example = Example->new;
317  
318 my $throw = $example->throw('Example::Error::Unknown');
319  
320 # bless({ "package" => "Example::Error::Unknown", ..., }, "Venus::Throw")
321  
322 # $throw->error;
323  
324 =back
325  
326 =over 4
327  
328 =item throw example 3
329  
330 package main;
331  
332 my $example = Example->new;
333  
334 my $throw = $example->throw({
335 name => 'on.example',
336 capture => [$example],
337 stash => {
338 time => time,
339 },
340 });
341  
342 # bless({ "package" => "Example::Error", ..., }, "Venus::Throw")
343  
344 # $throw->error;
345  
346 =back
347  
348 =over 4
349  
350 =item throw example 4
351  
352 # given: synopsis
353  
354 package Example;
355  
356 # ...
357  
358 sub error_on_example {
359 my ($self) = @_;
360  
361 return {
362 name => 'on.example',
363 capture => [$example],
364 stash => {
365 time => time,
366 },
367 };
368 }
369  
370 package main;
371  
372 my $throw = $example->throw('error_on_example');
373  
374 # bless({ "package" => "Example::Error", ..., }, "Venus::Throw")
375  
376 # $throw->error;
377  
378 =back
379  
380 =over 4
381  
382 =item throw example 5
383  
384 # given: synopsis
385  
386 package Example;
387  
388 # ...
389  
390 sub error_on_example {
391 my ($self) = @_;
392  
393 return {
394 name => 'on.example',
395 capture => [$example],
396 stash => {
397 time => time,
398 },
399 raise => 1,
400 };
401 }
402  
403 package main;
404  
405 my $throw = $example->throw('error_on_example');
406  
407 # Exception! (isa Example::Error)
408  
409 =back
410  
411 =cut
412  
413 =head1 AUTHORS
414  
415 Awncorp, C<awncorp@cpan.org>
416  
417 =cut
418  
419 =head1 LICENSE
420  
421 Copyright (C) 2000, Awncorp, C<awncorp@cpan.org>.
422  
423 This program is free software, you can redistribute it and/or modify it under
424 the terms of the Apache license version 2.0.
425  
426 =cut
« blib/lib/Venus/Opts.pm ↑ Summary blib/lib/Venus/Role/Printable.pm »