Future/Exception.pm

filter: Statementstmt 100% 25 / 25 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 42.8% 3 / 7 totaltotal 91.4% 43 / 47 CC 2 SCAR 1.3
CC
2
Cov
100%
throw
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Test/Future/Deferred.pm ↑ Summary
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2019-2026 -- leonerd@leonerd.org.uk
5  
6 package Future::Exception 0.54;
7  
8 33 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 33 ▶use feature qw( signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 33 ▶no warnings qw( experimental::signatures );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 C<Future::Exception> - an exception type for failed L<Future>s
17  
18 =head1 SYNOPSIS
19  
20 =for highlighter language=perl
21  
22 use Scalar::Util qw( blessed );
23 use Syntax::Keyword::Try;
24  
25 try {
26 my $f = ...;
27 my @result = $f->result;
28 ...
29 }
30 catch {
31 if( blessed($@) and $@->isa( "Future::Exception" ) {
32 print STDERR "The ", $@->category, " failed: ", $@->message, "\n";
33 }
34 }
35  
36 =head1 DESCRIPTION
37  
38 The C<get> method on a failed L<Future> instance will throw an exception to
39 indicate that the future failed. A failed future can contain a failure
40 category name and other details as well as the failure message, so in this
41 case the exception will be an instance of C<Future::Exception> to make these
42 values accessible.
43  
44 Users should not depend on exact class name matches, but instead rely on
45 inheritence, as a later version of this implementation might dynamically
46 create subclasses whose names are derived from the Future failure category
47 string, to assist with type matching. Note the use of C<< ->isa >> in the
48 SYNOPSIS example.
49  
50 =cut
51  
52 use overload
53 4 ▶ '""' => sub ( $self, $, $ ) { $self->message },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
54 33 fallback => 1;
55  
56 =head1 CONSTRUCTOR
57  
58 =head2 from_future
59  
60 $e = Future::Exception->from_future( $f );
61  
62 Constructs a new C<Future::Exception> wrapping the given failed future.
63  
64 =cut
65  
66 1 ▶sub from_future ( $class, $f )
Subroutine from_future: called CC=1 SCAR=0.0
Pod documented
67 {
68 1 return $class->new( $f->failure );
69 }
70  
71 5 ▶sub new ( $class, @args ) { bless [ @args ], $class; }
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
72  
73 =head1 ACCESSORS
74  
75 $message = $e->message;
76 $category = $e->category;
77 @details = $e->details;
78  
79 Additionally, the object will stringify to return the message value, for the
80 common use-case of printing, regexp testing, or other behaviours.
81  
82 =cut
83  
84 10 ▶sub message ( $self ) { $self->[0] }
Subroutine message: called CC=1 SCAR=0.0
Pod undocumented
85 5 ▶sub category ( $self ) { $self->[1] }
Subroutine category: called CC=1 SCAR=0.0
Pod undocumented
86 5 ▶sub details ( $self ) { @{$self}[2..$#$self] }
Subroutine details: called CC=1 SCAR=0.0
Pod undocumented
87  
88 =head1 METHODS
89  
90 =cut
91  
92 =head2 throw
93  
94 Future::Exception->throw( $message, $category, @details );
95  
96 I<Since version 0.41.>
97  
98 Constructs a new exception object and throws it using C<die()>. This method
99 will not return, as it raises the exception directly.
100  
101 If C<$message> does not end in a linefeed then the calling file and line
102 number are appended to it, in the same way C<die()> does.
103  
104 =cut
105  
106 2 ▶sub throw ( $class, $message, $category, @details )
Subroutine throw: called CC=2 SCAR=6.9
Pod documented
107 {
108 2 ▶ $message =~ m/\n$/ or
Branch: 1 branch
BranchTrueFalseTotal
$message =~ /\n$/u or $message .= sprintf(" at %s line %d.\n", (caller)[1, 2]) 1 1 2
109 $message .= sprintf " at %s line %d.\n", ( caller )[1,2];
110 2 die $class->new( $message, $category, @details );
111 }
112  
113 # TODO: consider a 'croak' method that uses Carp::shortmess to find a suitable
114 # file/linenumber
115  
116 =head2 as_future
117  
118 $f = $e->as_future;
119  
120 Returns a new C<Future> object in a failed state matching the exception.
121  
122 =cut
123  
124 1 ▶sub as_future ( $self )
Subroutine as_future: called CC=1 SCAR=0.0
Pod documented
125 {
126 1 return Future->fail( $self->message, $self->category, $self->details );
127 }
128  
129 =head1 AUTHOR
130  
131 Paul Evans <leonerd@leonerd.org.uk>
132  
133 =cut
134  
135 33 0x55AA;
« Test/Future/Deferred.pm ↑ Summary