| 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.07 . | ||
| 10 | |||
| 11 | =cut | ||
| 12 | |||
| 13 | |||
| 14 | |||
| 15 | use 5.010; | ||
| 16 | use strict; | ||
| 17 | use warnings; | ||
| 18 | |||
| 19 | |||
| 20 | |||
| 21 | our $DEBUG = 0; | ||
| 22 | |||
| 23 | our $VERSION = '1.07'; | ||
| 24 | |||
| 25 | use Exporter qw( | ||
| 26 | import | ||
| 27 | ); | ||
| 28 | our @EXPORT = qw( | ||
| 29 | throw_args | ||
| 30 | ); | ||
| 31 | |||
| 32 | |||
| 33 | |||
| 34 | use Exception::Class ( | ||
| 35 | '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 | ); | ||
| 59 | |||
| 60 | use Kafka qw( | ||
| 61 | %ERROR | ||
| 62 | ); | ||
| 63 | use Kafka::Internals qw( | ||
| 64 | format_message | ||
| 65 | ); | ||
| 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 | my $error_code = shift; | ||
| 215 | my $description = shift; | ||
| 216 | |||
| 217 | return ( | ||
| 218 | code => $error_code, | ||
| 219 | 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__ |