File Coverage

blib/lib/LINQ/Exception.pm
Criterion Covered Total %
statement 75 75 100.0
branch 6 6 100.0
condition n/a
subroutine 29 29 100.0
pod 3 3 100.0
total 113 113 100.0


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