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 version 1.07 .
10              
11             =cut
12              
13              
14              
15 16     16   241 use 5.010;
  16         46  
16 16     16   69 use strict;
  16         23  
  16         278  
17 16     16   61 use warnings;
  16         28  
  16         699  
18              
19              
20              
21             our $DEBUG = 0;
22              
23             our $VERSION = '1.07';
24              
25 16         1253 use Exporter qw(
26             import
27 16     16   70 );
  16         24  
28             our @EXPORT = qw(
29             throw_args
30             );
31              
32              
33              
34             use Exception::Class (
35 16         163 '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 16     16   4582 );
  16         57257  
59              
60 16         1353 use Kafka qw(
61             %ERROR
62 16     16   23338 );
  16         35  
63 16         2037 use Kafka::Internals qw(
64             format_message
65 16     16   2085 );
  16         29  
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 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 and its subclasses:
127              
128             =over 3
129              
130             =item C
131              
132             An error code that references error in C<%Kafka::ERROR> hash.
133              
134             =item C
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 subclass specific
145             to that module:
146              
147             =over 3
148              
149             =item C
150              
151             See L methods.
152              
153             =item C
154              
155             See L methods.
156              
157             =item C
158              
159             See L methods.
160              
161             =item C
162              
163             See L methods.
164              
165             =item C
166              
167             See L methods.
168              
169             =item C
170              
171             See L methods.
172              
173             =back
174              
175             Authors suggest using of L's C and C to handle exceptions while
176             working with L package.
177              
178             You may also want to review documentation of L,
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 module:
190              
191             =cut
192              
193             =head3 C
194              
195             Converts arguments into C constructor attributes L and L.
196              
197             C 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.
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 586     586 1 788 my $error_code = shift;
215 586         690 my $description = shift;
216              
217             return (
218             code => $error_code,
219 586 100       2191 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__