File Coverage

blib/lib/Ryu/Exception.pm
Criterion Covered Total %
statement 28 29 96.5
branch 5 10 50.0
condition 2 6 33.3
subroutine 11 12 91.6
pod 8 8 100.0
total 54 65 83.0


line stmt bran cond sub pod time code
1             package Ryu::Exception;
2              
3 1     1   73435 use strict;
  1         10  
  1         26  
4 1     1   4 use warnings;
  1         1  
  1         43  
5              
6             our $VERSION = '3.001'; # VERSION
7             our $AUTHORITY = 'cpan:TEAM'; # AUTHORITY
8              
9             =head1 NAME
10              
11             Ryu::Exception - support for L-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.
26              
27             =cut
28              
29 1     1   615 use Future;
  1         10786  
  1         31  
30 1     1   7 use Scalar::Util;
  1         2  
  1         288  
31              
32             =head2 new
33              
34             Instantiate from named parameters.
35              
36             =cut
37              
38 3     3 1 3581 sub new { bless { @_[1..$#_] }, $_[0] }
39              
40             =head2 throw
41              
42             Throws this exception.
43              
44             $exception->throw;
45              
46             =cut
47              
48 0     0 1 0 sub throw { die shift }
49              
50             =head2 type
51              
52             Returns the type, which should be a string such as C.
53              
54             =cut
55              
56 5     5 1 620 sub type { shift->{type} }
57              
58             =head2 message
59              
60             Returns the message, which is a freeform string.
61              
62             =cut
63              
64 5     5 1 2663 sub message { shift->{message} }
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 50   4 1 605 sub details { @{ shift->{details} || [] } }
  4         20  
73              
74             =head2 fail
75              
76             Fails the given L with this exception.
77              
78             =cut
79              
80             sub fail {
81 1     1 1 530 my ($self, $f) = @_;
82 1 50 33     13 die "expects a Future" unless Scalar::Util::blessed($f) && $f->isa('Future');
83 1         4 $self->as_future->on_ready($f);
84 1         103 $f;
85             }
86              
87             =head2 as_future
88              
89             Returns a failed L containing the message, type and details from
90             this exception.
91              
92             =cut
93              
94             sub as_future {
95 1     1 1 2 my ($self) = @_;
96 1         3 return Future->fail($self->message, $self->type, $self->details);
97             }
98              
99             =head2 from_future
100              
101             Extracts failure information from a L and instantiates accordingly.
102              
103             =cut
104              
105             sub from_future {
106 1     1 1 3 my ($class, $f) = @_;
107 1 50 33     9 die "expects a Future" unless Scalar::Util::blessed($f) && $f->isa('Future');
108 1 50       4 die "Future is not ready" unless $f->is_ready;
109 1 50       7 my ($msg, $type, @details) = $f->failure or die "Future is not failed?";
110 1         16 $class->new(
111             message => $msg,
112             type => $type,
113             details => \@details
114             )
115             }
116              
117             1;
118              
119             __END__