LINQ/Exception.pm

filter: Statementstmt 99.0% 101 / 102 Branchbran 90.0% 9 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 29 / 29 Podpod 100% 3 / 3 totaltotal 98.6% 142 / 144 CC 5 SCAR 2.2
CC
5
Cov
98%
throw
2.0
BUILD
2.0
BUILD
2.0
CRAP 1.2
« LINQ/Array.pm ↑ Summary LINQ/Grouping.pm »
1 39 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 39 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 39 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 39 ▶if ( $] < 5.010000 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < "5.01") 0 39 39
6 0 require UNIVERSAL::DOES;
7 }
8  
9 {
10  
11 package LINQ::Exception;
12 39 our $AUTHORITY = 'cpan:TOBYINK';
13 39 our $VERSION = '0.003';
14
15 39 ▶ use Class::Tiny qw( package file line );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 39 ▶ use overload q[""] => sub { shift->to_string };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
17
18 2 ▶ sub message { "An error occurred" }
Subroutine message: called CC=1 SCAR=0.0
Pod documented
19
20 60 ▶ sub to_string {
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
21 60 my $self = shift;
22 60 sprintf(
23 "%s at %s line %d.\n",
24 $self->message,
25 $self->file,
26 $self->line,
27 );
28 }
29
30 69 ▶ sub throw {
Subroutine throw: called CC=2 SCAR=6.9
Pod documented
31 69 my $class = shift;
32
33 69 my ( $level, %caller ) = 0;
34 69 ▶ $level++ until caller( $level ) !~ /\ALINQx?(::|\z)/;
Branch: 1 branch
BranchTrueFalseTotal
not caller($level) =~ /\ALINQx?(::|\z)/ or do { ++$level } 69 162 231
35 69 @caller{qw/ package file line /} = caller( $level );
36
37 69 die( $class->new( %caller, @_ ) );
38 }
39 }
40  
41 {
42  
43 39 package LINQ::Exception::Unimplemented;
44 39 our $AUTHORITY = 'cpan:TOBYINK';
45 39 our $VERSION = '0.003';
46 39 ▶ use parent -norequire, qw( LINQ::Exception );
Subroutine BEGIN: called CC=1 SCAR=0.0
47 39 ▶ use Class::Tiny qw( method );
Subroutine BEGIN: called CC=1 SCAR=0.0
48
49 4 ▶ sub message {
Subroutine message: called CC=1 SCAR=0.0
50 4 my $self = shift;
51 4 my $meth = $self->method;
52 4 "Method $meth is unimplemented";
53 }
54 }
55  
56 {
57  
58 39 package LINQ::Exception::InternalError;
59 39 our $AUTHORITY = 'cpan:TOBYINK';
60 39 our $VERSION = '0.003';
61 39 ▶ use parent -norequire, qw( LINQ::Exception );
Subroutine BEGIN: called CC=1 SCAR=0.0
62 39 ▶ use Class::Tiny qw( message );
Subroutine BEGIN: called CC=1 SCAR=0.0
63 }
64  
65 {
66  
67 39 package LINQ::Exception::CallerError;
68 39 our $AUTHORITY = 'cpan:TOBYINK';
69 39 our $VERSION = '0.003';
70 39 ▶ use parent -norequire, qw( LINQ::Exception );
Subroutine BEGIN: called CC=1 SCAR=0.0
71 39 ▶ use Class::Tiny qw( message );
Subroutine BEGIN: called CC=1 SCAR=0.0
72
73 28 ▶ sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
74 28 my $self = shift;
75 28 ▶ 'LINQ::Exception::InternalError'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->message 1 27 28
76 ->throw( message => 'Required attribute "message" not defined' )
77 unless defined $self->message;
78 }
79 }
80  
81 {
82  
83 39 package LINQ::Exception::CollectionError;
84 39 our $AUTHORITY = 'cpan:TOBYINK';
85 39 our $VERSION = '0.003';
86 39 ▶ use parent -norequire, qw( LINQ::Exception );
Subroutine BEGIN: called CC=1 SCAR=0.0
87 39 ▶ use Class::Tiny qw( collection );
Subroutine BEGIN: called CC=1 SCAR=0.0
88
89 31 ▶ sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
90 31 my $self = shift;
91 31 ▶ 'LINQ::Exception::InternalError'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->collection 2 29 31
92 ->throw( message => 'Required attribute "collection" not defined' )
93 unless defined $self->collection;
94 }
95 }
96  
97 {
98  
99 39 package LINQ::Exception::NotFound;
100 39 our $AUTHORITY = 'cpan:TOBYINK';
101 39 our $VERSION = '0.003';
102 39 ▶ use parent -norequire, qw( LINQ::Exception::CollectionError );
Subroutine BEGIN: called CC=1 SCAR=0.0
103 10 ▶ sub message { "Item not found" }
Subroutine message: called CC=1 SCAR=0.0
104 }
105  
106 {
107  
108 39 package LINQ::Exception::MultipleFound;
109 39 our $AUTHORITY = 'cpan:TOBYINK';
110 39 our $VERSION = '0.003';
111 39 ▶ use parent -norequire, qw( LINQ::Exception::CollectionError );
Subroutine BEGIN: called CC=1 SCAR=0.0
112 39 ▶ use Class::Tiny qw( found );
Subroutine BEGIN: called CC=1 SCAR=0.0
113 2 ▶ sub message { "Item not found" }
Subroutine message: called CC=1 SCAR=0.0
114 }
115  
116 {
117  
118 39 package LINQ::Exception::Cast;
119 39 our $AUTHORITY = 'cpan:TOBYINK';
120 39 our $VERSION = '0.003';
121 39 ▶ use parent -norequire, qw( LINQ::Exception::CollectionError );
Subroutine BEGIN: called CC=1 SCAR=0.0
122 39 ▶ use Class::Tiny qw( type );
Subroutine BEGIN: called CC=1 SCAR=0.0
123
124 4 ▶ sub message {
Subroutine message: called CC=1 SCAR=0.0
125 4 my $type = shift->type;
126 4 "Not all elements in the collection could be cast to $type";
127 }
128
129 5 ▶ sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
130 5 my $self = shift;
131 5 ▶ 'LINQ::Exception::InternalError'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->type 1 4 5
132 ->throw( message => 'Required attribute "type" not defined' )
133 unless defined $self->type;
134 }
135 }
136  
137 39 1;
138  
139 =pod
140  
141 =encoding utf-8
142  
143 =head1 NAME
144  
145 LINQ::Exception - exceptions thrown by LINQ
146  
147 =head1 DESCRIPTION
148  
149 When LINQ encounters an error, it doesn't just C<die> with a string, but throws
150 an exception object which can be caught with C<eval>, L<Try::Tiny>, or
151 L<Syntax::Keyword::Try>.
152  
153 These objects overload stringification, so if they are not caught and dealt
154 with, you'll get a sensible error message printed.
155  
156 =head1 EXCEPTION TYPES
157  
158 =head2 LINQ::Exception
159  
160 This is the base class for all LINQ exceptions.
161  
162 use LINQ qw( LINQ );
163 use Syntax::Keyword::Try qw( try :experimental );
164
165 try {
166 my $collection = LINQ [ 1, 2, 3 ];
167 my $item = $collection->element_at( 10 );
168 }
169 catch ( $e isa LINQ::Exception ) {
170 printf(
171 "Got error: %s at %s (%s line %d)\n",
172 $e->message,
173 $e->package,
174 $e->file,
175 $e->line,
176 );
177 }
178  
179 The class provides C<message>, C<package>, C<file>, and C<line> methods to
180 get details of the error, as well as a C<to_string> method which provides the
181 message, package, file, and line as one combined string.
182  
183 There is a class method C<throw> which instantiates a new object and dies.
184  
185 'LINQ::Exception'->throw;
186  
187 LINQ::Exception is never directly thrown by LINQ, but subclasses of it are.
188  
189 =begin trustme
190  
191 =item throw
192  
193 =item message
194  
195 =item file
196  
197 =item line
198  
199 =item package
200  
201 =item to_string
202  
203 =end trustme
204  
205 =head2 LINQ::Exception::Unimplemented
206  
207 A subclass of LINQ::Exception thrown when you call a method or feature which
208 is not implemented for the collection you call it on.
209  
210 =head2 LINQ::Exception::InternalError
211  
212 A subclass of LINQ::Exception thrown when an internal error is encountered in
213 LINQ, not caused by the caller.
214  
215 =head2 LINQ::Exception::CallerError
216  
217 A subclass of LINQ::Exception thrown when the caller of a method has called it
218 incorrectly. For example, if a method is called which expects a coderef as a
219 parameter, but is given a string.
220  
221 =head2 LINQ::Exception::CollectionError
222  
223 A subclass of LINQ::Exception thrown when a method you've called cannot be
224 fulfilled by the collection you've called it on. For example, you've asked to
225 fetch the third item in a collection containing only two items.
226  
227 The exception has a C<collection> attribute which returns the collection which
228 generated the error.
229  
230 =head2 LINQ::Exception::NotFound
231  
232 A subclass of LINQ::Exception::CollectionError thrown when trying to access an
233 item in a collection which cannot be found.
234  
235 =head2 LINQ::Exception::MultipleFound
236  
237 A subclass of LINQ::Exception::CollectionError thrown when trying to access a
238 single item in a collection when multiple items are found.
239  
240 =head2 LINQ::Exception::Cast
241  
242 A subclass of LINQ::Exception::CollectionError thrown when trying to cast all
243 items in a collection to a type, but this fails for one or more items.
244  
245 =head1 BUGS
246  
247 Please report any bugs to
248 L<http://rt.cpan.org/Dist/Display.html?Queue=LINQ>.
249  
250 =head1 SEE ALSO
251  
252 L<LINQ>.
253  
254 =head1 AUTHOR
255  
256 Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
257  
258 =head1 COPYRIGHT AND LICENCE
259  
260 This software is copyright (c) 2014, 2021 by Toby Inkster.
261  
262 This is free software; you can redistribute it and/or modify it under
263 the same terms as the Perl 5 programming language system itself.
264  
265 =head1 DISCLAIMER OF WARRANTIES
266  
267 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
268 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
269 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
« LINQ/Array.pm ↑ Summary LINQ/Grouping.pm »