ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Net-XMPP2/lib/Net/XMPP2/Error.pm
Revision: 1.3
Committed: Tue Apr 24 17:37:21 2007 UTC (19 years, 6 months ago) by elmex
Branch: MAIN
Changes since 1.2: +37 -0 lines
Log Message:
fixed a minor bug in binding error handling and further work
on the in-band-registration stuff

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