ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Net-XMPP2/lib/Net/XMPP2/Error.pm
Revision: 1.4
Committed: Wed Apr 25 17:35:14 2007 UTC (19 years, 5 months ago) by elmex
Branch: MAIN
Changes since 1.3: +17 -1 lines
Log Message:
implemented IQ timeouts.

File Contents

# Content
1 package Net::XMPP2::Error;
2 use strict;
3 use Net::XMPP2::Util qw/stringprep_jid prep_bare_jid/;
4 use Net::XMPP2::Error;
5
6 =head1 NAME
7
8 Net::XMPP2::Error - An error class hierarchy for error reporting
9
10 =head1 SYNOPSIS
11
12 die $error->string;
13
14 =head1 DESCRIPTION
15
16 This module is a helper class for abstracting any kind
17 of error that occurs in Net::XMPP2.
18
19 You receive instances of these objects by various events.
20
21 =cut
22
23 sub new {
24 my $this = shift;
25 my $class = ref($this) || $this;
26 my $self = bless { @_ }, $class;
27 $self->init;
28 $self
29 }
30
31 sub init { }
32
33 =head1 SUPER CLASS
34
35 Net::XMPP2::Error - The super class of all errors
36
37 =head2 METHODS
38
39 These methods are implemented by all subclasses.
40
41 =head3 string ()
42
43 Returns a humand readable string for this error.
44
45 =cut
46
47 sub string {
48 my ($self) = @_;
49 $self->{text}
50 }
51
52 package Net::XMPP2::Error::IQ;
53 our @ISA = qw/Net::XMPP2::Error/;
54
55 =head1 SUBCLASS
56
57 Net::XMPP2::Error::IQ - IQ errors
58
59 =cut
60
61 sub init {
62 my ($self) = @_;
63 my $node = $self->xml_node;
64
65 unless (defined $node) {
66 $self->{iq_error_cond} = 'client-timeout';
67 $self->{iq_error_type} = 'cancel';
68 return;
69 }
70
71 my @error;
72 my ($err) = $node->find_all ([qw/client error/]);
73
74 unless ($err) {
75 warn "No error element found in error stanza!";
76 $self->{text} = "Unknown IQ error";
77 return
78 }
79
80 $self->{iq_error_type} = $err->attr ('type');
81 $self->{iq_error_code} = $err->attr ('code');
82
83 if (my ($txt) = $err->find_all ([qw/stanzas text/])) {
84 $self->{iq_error_text} = $txt->text;
85 }
86
87 for my $er (
88 qw/bad-request conflict feature-not-implemented forbidden
89 gone internal-server-error item-not-found jid-malformed
90 not-acceptable not-allowed not-authorized payment-required
91 recipient-unavailable redirect registration-required
92 remote-server-not-found remote-server-timeout resource-constraint
93 service-unavailable subscription-required undefined-condition
94 unexpected-request/)
95 {
96 if (my ($el) = $err->find_all ([stanzas => $er])) {
97 $self->{iq_error_cond} = $er;
98 $self->{iq_error_cond_node} = $el;
99 last;
100 }
101 }
102 }
103
104 =head2 METHODS
105
106 =head3 xml_node ()
107
108 Returns the L<Net::XMPP2::Node> object for this IQ error.
109 This method returns undef if the IQ timeouted.
110
111 In the case of a timeout the C<condition> method returns C<client-timeout>,
112 C<type> returns 'cancel' and C<code> undef.
113
114 =cut
115
116 sub xml_node {
117 $_[0]->{node}
118 }
119
120 =head3
121
122 =head3 type ()
123
124 This method returns one of:
125
126 'cancel', 'continue', 'modify', 'auth' and 'wait'
127
128 =cut
129
130 sub type { $_[0]->{iq_error_type} }
131
132 =head3 code ()
133
134 This method returns the error code if one was found.
135
136 =cut
137
138 sub code { $_[0]->{iq_error_code} }
139
140 =head3 condition ()
141
142 Returns the error condition string if one was found when receiving the IQ error.
143 It can be undef or one of:
144
145 bad-request
146 conflict
147 feature-not-implemented
148 forbidden
149 gone
150 internal-server-error
151 item-not-found
152 jid-malformed
153 not-acceptable
154 not-allowed
155 not-authorized
156 payment-required
157 recipient-unavailable
158 redirect
159 registration-required
160 remote-server-not-found
161 remote-server-timeout
162 resource-constraint
163 service-unavailable
164 subscription-required
165 undefined-condition
166 unexpected-request
167
168 Or, in case of a IQ timeout it returns:
169
170 'client-timeout'
171
172 =cut
173
174 sub condition { $_[0]->{iq_error_cond} }
175
176 =head3 condition_node ()
177
178 Returns the error condition node if one was found when receiving the IQ error.
179 This is mostly for debugging purposes.
180
181 =cut
182
183 sub condition_node { $_[0]->{iq_error_cond_node} }
184
185 =head3 text ()
186
187 The humand readable error portion. Might be undef if none was received.
188
189 =cut
190
191 sub text { $_[0]->{iq_error_text} }
192
193 sub string {
194 my ($self) = @_;
195
196 sprintf "iq error: %s/%s (type %s): %s",
197 $self->code || '',
198 $self->condition || '',
199 $self->type,
200 $self->text
201 }
202
203 package Net::XMPP2::Error::Stream;
204 our @ISA = qw/Net::XMPP2::Error/;
205
206 =head1 SUBCLASS
207
208 Net::XMPP2::Error::Stream - XML Stream errors
209
210 =cut
211
212 sub init {
213 my ($self) = @_;
214 my $node = $self->xml_node;
215
216 my @txt = $node->find_all ([qw/stream text/]);
217 my $error;
218 for my $er (
219 qw/bad-format bad-namespace-prefix conflict connection-timeout host-gone
220 host-unknown improper-addressing internal-server-error invalid-from
221 invalid-id invalid-namespace invalid-xml not-authorized policy-violation
222 remote-connection-failed resource-constraint restricted-xml
223 see-other-host system-shutdown undefined-condition unsupported-stanza-type
224 unsupported-version xml-not-well-formed/)
225 {
226 for ($node->nodes) {
227 if ($node->eq (streams => $er)) {
228 $error = $_->name;
229 last
230 }
231 }
232 }
233
234 unless ($error) {
235 #d# warn "got undefined error stanza, trying to find any undefined error...";
236 for ($node->nodes) {
237 if ($node->eq_ns ('streams')) {
238 $error = $node->name;
239 }
240 }
241 }
242
243 $self->{error_name} = $error;
244 $self->{error_text} = @txt ? $txt[0]->text : '';
245 }
246
247 =head2 METHODS
248
249 =head3 xml_node ()
250
251 Returns the L<Net::XMPP2::Node> object for this stream error.
252
253 =cut
254
255 sub xml_node {
256 $_[0]->{node}
257 }
258
259 =head3 name ()
260
261 Returns the name of the error. That might be undef, one of the following
262 strings or some other string that has been discovered by a heuristic
263 (because some servers send errors that are not in the RFC).
264
265 bad-format
266 bad-namespace-prefix
267 conflict
268 connection-timeout
269 host-gone
270 host-unknown
271 improper-addressing
272 internal-server-error
273 invalid-from
274 invalid-id
275 invalid-namespace
276 invalid-xml
277 not-authorized
278 policy-violation
279 remote-connection-failed
280 resource-constraint
281 restricted-xml
282 see-other-host
283 system-shutdown
284 undefined-condition
285 unsupported-stanza-type
286 unsupported-version
287 xml-not-well-formed
288
289 =cut
290
291 sub name { $_[0]->{error_name} }
292
293 =head3 text ()
294
295 The humand readable error portion. Might be undef if none was received.
296
297 =cut
298
299 sub text { $_[0]->{error_text} }
300
301 sub string {
302 my ($self) = @_;
303
304 sprintf "stream error: %s: %s",
305 $self->name,
306 $self->text
307 }
308
309 package Net::XMPP2::Error::SASL;
310 our @ISA = qw/Net::XMPP2::Error/;
311
312 =head1 SUBCLASS
313
314 Net::XMPP2::Error::SASL - SASL authentication error
315
316 =cut
317
318 sub init {
319 my ($self) = @_;
320 my $node = $self->xml_node;
321
322 my $error;
323 for ($node->nodes) {
324 $error = $_->name;
325 last
326 }
327
328 $self->{error_cond} = $error;
329 }
330
331 =head2 METHODS
332
333 =head3 xml_node ()
334
335 Returns the L<Net::XMPP2::Node> object for this stream error.
336
337 =cut
338
339 sub xml_node {
340 $_[0]->{node}
341 }
342
343 =head3 condition ()
344
345 Returns the error condition, which might be one of:
346
347 aborted
348 incorrect-encoding
349 invalid-authzid
350 invalid-mechanism
351 mechanism-too-weak
352 not-authorized
353 temporary-auth-failure
354
355 =cut
356
357 sub condition {
358 $_[0]->{error_cond}
359 }
360
361 sub string {
362 my ($self) = @_;
363
364 sprintf "sasl error: %s",
365 $self->condition
366 }
367
368 package Net::XMPP2::Error::Register;
369 our @ISA = qw/Net::XMPP2::Error::IQ/;
370
371 =head1 SUBCLASS
372
373 Net::XMPP2::Error::Register - In band registration error
374
375 =cut
376
377 =head2 METHODS
378
379 =head3 register_state ()
380
381 Returns the state of registration, one of:
382
383 form-request
384 form-submitted
385
386 =cut
387
388 sub register_state {
389 my ($self) = @_;
390 $self->{register_state}
391 }
392
393 sub string {
394 my ($self) = @_;
395
396 sprintf "ibb registration error (in %s): %s",
397 $self->register_state,
398 $self->SUPER::string
399 }
400
401 =head1 AUTHOR
402
403 Robin Redeker, C<< <elmex at ta-sa.org> >>
404
405 =head1 COPYRIGHT & LICENSE
406
407 Copyright 2007 Robin Redeker, all rights reserved.
408
409 This program is free software; you can redistribute it and/or modify it
410 under the same terms as Perl itself.
411
412 =cut
413
414 1; # End of Net::XMPP2