File Coverage

lib/Kafka/Exceptions.pm
Criterion Covered Total %
statement 23 23 100.0
branch 2 2 100.0
condition n/a
subroutine 8 8 100.0
pod 1 1 100.0
total 34 34 100.0


line stmt bran cond sub pod time code
1             package Kafka::Exceptions;
2              
3             =head1 NAME
4              
5             Kafka::Exceptions - Perl Kafka API exception definitions.
6              
7             =head1 VERSION
8              
9             This documentation refers to C<Kafka::Exceptions> version 1.08 .
10              
11             =cut
12              
13              
14              
15 17     17   314 use 5.010;
  17         90  
16 17     17   84 use strict;
  17         105  
  17         476  
17 17     17   145 use warnings;
  17         46  
  17         994  
18              
19              
20              
21             our $DEBUG = 0;
22              
23             our $VERSION = 'v1.08';
24              
25 17         1974 use Exporter qw(
26             import
27 17     17   102 );
  17         40  
28             our @EXPORT = qw(
29             throw_args
30             );
31              
32              
33              
34             use Exception::Class (
35 17         225 'Kafka::Exception' => {
36             fields => [ 'code', 'message' ],
37             },
38             'Kafka::Exception::Connection' => {
39             isa => 'Kafka::Exception',
40             fields => [ 'request', 'response', 'io_error' ],
41             },
42             'Kafka::Exception::Consumer' => {
43             isa => 'Kafka::Exception',
44             },
45             'Kafka::Exception::Int64' => {
46             isa => 'Kafka::Exception',
47             },
48             'Kafka::Exception::IO' => {
49             fields => [ 'errno' ],
50             isa => 'Kafka::Exception',
51             },
52             'Kafka::Exception::Producer' => {
53             isa => 'Kafka::Exception',
54             },
55             'Kafka::Exception::Protocol' => {
56             isa => 'Kafka::Exception',
57             },
58 17     17   8882 );
  17         88356  
59              
60 17         1621 use Kafka qw(
61             %ERROR
62 17     17   32115 );
  17         45  
63 17         2670 use Kafka::Internals qw(
64             format_message
65 17     17   3438 );
  17         39  
66              
67             Kafka::Exception->Trace(1); # include stack traces
68              
69              
70             =head1 SYNOPSIS
71              
72             use 5.010;
73             use strict;
74             use warnings;
75              
76             use Scalar::Util qw(
77             blessed
78             );
79             use Try::Tiny;
80              
81             # A simple example of Kafka::Connection usage:
82             use Kafka::Connection;
83              
84             # connect to local cluster with the defaults
85             my $connection;
86             try {
87             $connection = Kafka::Connection->new( host => 'localhost' );
88             } catch {
89             my $error = $_;
90             if ( blessed( $error ) && $error->isa( 'Kafka::Exception' ) ) {
91             if ( $error->isa( 'Kafka::Exception::Connection' ) ) {
92             # Specific treatment for 'Kafka::Connection' class error
93             } elsif ( $error->isa( 'Kafka::Exception::IO' ) ) {
94             # Specific treatment for 'Kafka::IO' class error
95             }
96             warn ref( $error ), " error:\n", $error->message, "\n", $error->trace->as_string, "\n";
97             exit;
98             } else {
99             die $error;
100             }
101             };
102              
103             # Closes the connection and cleans up
104             $connection->close;
105             undef $connection;
106              
107             =head1 DESCRIPTION
108              
109             The purpose of the C<Kafka::Exceptions> module is:
110              
111             =over 3
112              
113             =item *
114              
115             Declare a Kafka API exceptions hierarchy.
116              
117             =item *
118              
119             Provide additional methods for working with exceptions.
120              
121             =back
122              
123             It is designed to make exception handling structured, simpler and better by encouraging use
124             of hierarchy of exceptions in application (vs single catch-all exception class).
125              
126             The following additional attributes are available in C<Kafka::Exception> and its subclasses:
127              
128             =over 3
129              
130             =item C<code>
131              
132             An error code that references error in C<%Kafka::ERROR> hash.
133              
134             =item C<message>
135              
136             An error message that contains information about the encountered failure.
137             This message may contain additional details which are not provided by C<%Kafka::ERROR> hash.
138              
139             =back
140              
141             Exception objects provide accessor methods for these attributes. Attributes are inherited by
142             subclasses.
143              
144             Various Kafka API modules throw exceptions objects of a C<Kafka::Exception> subclass specific
145             to that module:
146              
147             =over 3
148              
149             =item C<Kafka::Exception::Connection>
150              
151             See L<Kafka::Connection|Kafka::Connection> methods.
152              
153             =item C<Kafka::Exception::Consumer>
154              
155             See L<Kafka::Consumer|Kafka::Consumer> methods.
156              
157             =item C<Kafka::Exception::IO>
158              
159             See L<Kafka::IO|Kafka::IO> methods.
160              
161             =item C<Kafka::Exception::Int64>
162              
163             See L<Kafka::Int64|Kafka::Int64> methods.
164              
165             =item C<Kafka::Exception::Producer>
166              
167             See L<Kafka::Producer|Kafka::Producer> methods.
168              
169             =item C<Kafka::Exception::Protocol>
170              
171             See L<Kafka::Protocol|Kafka::Protocol> methods.
172              
173             =back
174              
175             Authors suggest using of L<Try::Tiny|Try::Tiny>'s C<try> and C<catch> to handle exceptions while
176             working with L<Kafka|Kafka> package.
177              
178             You may also want to review documentation of L<Exception::Class|Exception::Class>,
179             which is the default base class for all exception objects created by this module.
180              
181             =cut
182              
183             #-- constructor ----------------------------------------------------------------
184              
185             #-- public attributes ----------------------------------------------------------
186              
187             =head2 FUNCTIONS
188              
189             The following functions are exported by C<Kafka::Exceptions> module:
190              
191             =cut
192              
193             =head3 C<throw_args( $error_code, $description )>
194              
195             Converts arguments into C<Kafka::Exception> constructor attributes L</code> and L</message>.
196              
197             C<throw_args()> accepts the following arguments:
198              
199             =over 3
200              
201             =item C<$error_code>
202              
203             The code of the last error.
204             The code must match the error codes defined in the module L<Kafka|Kafka>.
205              
206             =item C<$description>
207              
208             An additional error description that contains information about the encountered problem.
209              
210             =back
211              
212             =cut
213             sub throw_args {
214 585     585 1 909 my $error_code = shift;
215 585         713 my $description = shift;
216              
217             return (
218             code => $error_code,
219 585 100       2610 message => format_message( '%s%s', $ERROR{ $error_code }, $description ? ": $description" : '' ),
220             @_,
221             );
222             }
223              
224             #-- private attributes ---------------------------------------------------------
225              
226             #-- private methods ------------------------------------------------------------
227              
228              
229              
230             1;
231              
232             __END__
233              
234             =head1 SEE ALSO
235              
236             The basic operation of the Kafka package modules:
237              
238             L<Kafka|Kafka> - constants and messages used by the Kafka package modules.
239              
240             L<Kafka::Connection|Kafka::Connection> - interface to connect to a Kafka cluster.
241              
242             L<Kafka::Producer|Kafka::Producer> - interface for producing client.
243              
244             L<Kafka::Consumer|Kafka::Consumer> - interface for consuming client.
245              
246             L<Kafka::Message|Kafka::Message> - interface to access Kafka message
247             properties.
248              
249             L<Kafka::Int64|Kafka::Int64> - functions to work with 64 bit elements of the
250             protocol on 32 bit systems.
251              
252             L<Kafka::Protocol|Kafka::Protocol> - functions to process messages in the
253             Apache Kafka's Protocol.
254              
255             L<Kafka::IO|Kafka::IO> - low-level interface for communication with Kafka server.
256              
257             L<Kafka::Exceptions|Kafka::Exceptions> - module designated to handle Kafka exceptions.
258              
259             L<Kafka::Internals|Kafka::Internals> - internal constants and functions used
260             by several package modules.
261              
262             A wealth of detail about the Apache Kafka and the Kafka Protocol:
263              
264             Main page at L<http://kafka.apache.org/>
265              
266             Kafka Protocol at L<https://cwiki.apache.org/confluence/display/KAFKA/A+Guide+To+The+Kafka+Protocol>
267              
268             =head1 SOURCE CODE
269              
270             Kafka package is hosted on GitHub:
271             L<https://github.com/TrackingSoft/Kafka>
272              
273             =head1 AUTHOR
274              
275             Sergey Gladkov
276              
277             Please use GitHub project link above to report problems or contact authors.
278              
279             =head1 CONTRIBUTORS
280              
281             Alexander Solovey
282              
283             Jeremy Jordan
284              
285             Sergiy Zuban
286              
287             Vlad Marchenko
288              
289             =head1 COPYRIGHT AND LICENSE
290              
291             Copyright (C) 2012-2017 by TrackingSoft LLC.
292              
293             This package is free software; you can redistribute it and/or modify it under
294             the same terms as Perl itself. See I<perlartistic> at
295             L<http://dev.perl.org/licenses/artistic.html>.
296              
297             This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
298             without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
299             PARTICULAR PURPOSE.
300              
301             =cut