Ryu/Exception.pm

filter: Statementstmt 96.8% 31 / 32 Branchbran 50.0% 4 / 8 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 91.6% 11 / 12 Podpod 100% 8 / 8 totaltotal 78.0% 57 / 73 CC 8 SCAR 10.6
CC
8
Cov
79%
from_future
6.1
fail
3.3
details
2.1
CRAP 2.9
« Ryu/Buffer.pm ↑ Summary Ryu/Observable.pm »
1 package Ryu::Exception;
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 our $VERSION = '4.001'; # VERSION
7 2 our $AUTHORITY = 'cpan:TEAM'; # AUTHORITY
8  
9 =head1 NAME
10  
11 Ryu::Exception - support for L<Future>-style failure information
12  
13 =head1 SYNOPSIS
14  
15 use Ryu::Exception;
16 my $exception = Ryu::Exception->new(
17 type => 'http',
18 message => '404 response'
19 details => [ $response, $request ]
20 );
21 Future->fail($exception->failure);
22  
23 =head1 DESCRIPTION
24  
25 Generic exceptions interface, implements the 3-part failure codes as described in L<Future>.
26  
27 =cut
28  
29 2 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 =head2 new
33  
34 Instantiate from named parameters.
35  
36 =cut
37  
38 3 ▶sub new { bless { @_[1..$#_] }, $_[0] }
Subroutine new: called CC=1 SCAR=0.0
Pod documented
39  
40 =head2 throw
41  
42 Throws this exception.
43  
44 $exception->throw;
45  
46 =cut
47  
48 0 ▶sub throw { die shift }
Subroutine throw: not called CC=1 SCAR=6.9
Pod documented
49  
50 =head2 type
51  
52 Returns the type, which should be a string such as C<http>.
53  
54 =cut
55  
56 5 ▶sub type { shift->{type} }
Subroutine type: called CC=1 SCAR=0.0
Pod documented
57  
58 =head2 message
59  
60 Returns the message, which is a freeform string.
61  
62 =cut
63  
64 5 ▶sub message { shift->{message} }
Subroutine message: called CC=1 SCAR=0.0
Pod documented
65  
66 =head2 details
67  
68 Returns the list of details, the specifics of which are specific to the type.
69  
70 =cut
71  
72 4 ▶sub details { @{ shift->{details} || [] } }
Subroutine details: called CC=2 SCAR=7.2
Pod documented
Condition50%1/2
+(shift())->{'details'} || []
l!l
40
MC/DC0%0/1
+(shift())->{'details'} || []
+(shift())->{'details'}
Truth table50%1/2
A
A= +(shift())->{'details'}
Aresult
11
00
73  
74 =head2 fail
75  
76 Fails the given L<Future> with this exception.
77  
78 =cut
79  
80 1 ▶sub fail {
Subroutine fail: called CC=3 SCAR=12.0
Pod documented
81 1 my ($self, $f) = @_;
82 1 ▶ die "expects a Future" unless Scalar::Util::blessed($f) && $f->isa('Future');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($f) and $f->isa("Future") 0 1 1
Condition33%1/3
builtin::blessed($f) and $f->isa("Future")
!ll&&!rl&&r
001
MC/DC0%0/2
builtin::blessed($f) and $f->isa("Future")
builtin::blessed($f) $f->isa("Future")
Truth table33%1/3
A and B
A= builtin::blessed($f)B= $f->isa("Future")
ABresult
0X0
100
111
83 1 $self->as_future->on_ready($f);
84 1 $f;
85 }
86  
87 =head2 as_future
88  
89 Returns a failed L<Future> containing the message, type and details from
90 this exception.
91  
92 =cut
93  
94 1 ▶sub as_future {
Subroutine as_future: called CC=1 SCAR=0.0
Pod documented
95 1 my ($self) = @_;
96 1 return Future->fail($self->message, $self->type, $self->details);
97 }
98  
99 =head2 from_future
100  
101 Extracts failure information from a L<Future> and instantiates accordingly.
102  
103 =cut
104  
105 1 ▶sub from_future {
Subroutine from_future: called CC=5 SCAR=18.1
Pod documented
106 1 my ($class, $f) = @_;
107 1 ▶ die "expects a Future" unless Scalar::Util::blessed($f) && $f->isa('Future');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($f) and $f->isa("Future") 0 1 1
Condition33%1/3
builtin::blessed($f) and $f->isa("Future")
!ll&&!rl&&r
001
MC/DC0%0/2
builtin::blessed($f) and $f->isa("Future")
builtin::blessed($f) $f->isa("Future")
Truth table33%1/3
A and B
A= builtin::blessed($f)B= $f->isa("Future")
ABresult
0X0
100
111
108 1 ▶ die "Future is not ready" unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 0 1 1
109 1 ▶ my ($msg, $type, @details) = $f->failure or die "Future is not failed?";
Branch: 1 branch
BranchTrueFalseTotal
() = $f->failure or die "Future is not failed?" 1 0 1
110 1 $class->new(
111 message => $msg,
112 type => $type,
113 details => \@details
114 )
115 }
116  
117 2 1;
118  
119 __END__
« Ryu/Buffer.pm ↑ Summary Ryu/Observable.pm »