ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Net-XMPP2/lib/Net/XMPP2/Error.pm
Revision: 1.5
Committed: Mon Jun 25 08:32:00 2007 UTC (19 years, 3 months ago) by elmex
Branch: MAIN
Changes since 1.4: +9 -11 lines
Log Message:
fixed stream error

File Contents

# User Rev Content
1 elmex 1.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 elmex 1.4 unless (defined $node) {
66     $self->{iq_error_cond} = 'client-timeout';
67     $self->{iq_error_type} = 'cancel';
68     return;
69     }
70    
71 elmex 1.1 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 elmex 1.4 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 elmex 1.1
114     =cut
115    
116     sub xml_node {
117     $_[0]->{node}
118     }
119    
120 elmex 1.4 =head3
121    
122 elmex 1.1 =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 elmex 1.4 =head3 condition ()
141 elmex 1.1
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 elmex 1.4 Or, in case of a IQ timeout it returns:
169    
170     'client-timeout'
171    
172 elmex 1.1 =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 elmex 1.2 package Net::XMPP2::Error::Stream;
204 elmex 1.1 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 elmex 1.5 my @txt = $node->find_all ([qw/streams text/]);
217 elmex 1.1 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 elmex 1.5 if (my (@n) = $node->find_all ([streams => $er])) {
227     $error = $n[0]->name;
228     last;
229 elmex 1.1 }
230     }
231    
232     unless ($error) {
233     #d# warn "got undefined error stanza, trying to find any undefined error...";
234 elmex 1.5 for my $n ($node->nodes) {
235     if ($n->eq_ns ('streams')) {
236     $error = $n->name;
237 elmex 1.1 }
238     }
239     }
240    
241     $self->{error_name} = $error;
242     $self->{error_text} = @txt ? $txt[0]->text : '';
243     }
244    
245 elmex 1.3 =head2 METHODS
246    
247 elmex 1.1 =head3 xml_node ()
248    
249     Returns the L<Net::XMPP2::Node> object for this stream error.
250    
251     =cut
252    
253     sub xml_node {
254     $_[0]->{node}
255     }
256    
257     =head3 name ()
258    
259     Returns the name of the error. That might be undef, one of the following
260     strings or some other string that has been discovered by a heuristic
261     (because some servers send errors that are not in the RFC).
262    
263     bad-format
264     bad-namespace-prefix
265     conflict
266     connection-timeout
267     host-gone
268     host-unknown
269     improper-addressing
270     internal-server-error
271     invalid-from
272     invalid-id
273     invalid-namespace
274     invalid-xml
275     not-authorized
276     policy-violation
277     remote-connection-failed
278     resource-constraint
279     restricted-xml
280     see-other-host
281     system-shutdown
282     undefined-condition
283     unsupported-stanza-type
284     unsupported-version
285     xml-not-well-formed
286    
287     =cut
288    
289 elmex 1.2 sub name { $_[0]->{error_name} }
290 elmex 1.1
291     =head3 text ()
292    
293     The humand readable error portion. Might be undef if none was received.
294    
295     =cut
296    
297     sub text { $_[0]->{error_text} }
298    
299     sub string {
300     my ($self) = @_;
301    
302 elmex 1.5 sprintf ("stream error: %s: %s",
303 elmex 1.1 $self->name,
304 elmex 1.5 $self->text)
305 elmex 1.1 }
306    
307 elmex 1.2 package Net::XMPP2::Error::SASL;
308     our @ISA = qw/Net::XMPP2::Error/;
309    
310     =head1 SUBCLASS
311    
312     Net::XMPP2::Error::SASL - SASL authentication error
313    
314     =cut
315    
316     sub init {
317     my ($self) = @_;
318     my $node = $self->xml_node;
319    
320     my $error;
321     for ($node->nodes) {
322     $error = $_->name;
323     last
324     }
325    
326     $self->{error_cond} = $error;
327     }
328    
329 elmex 1.3 =head2 METHODS
330    
331 elmex 1.2 =head3 xml_node ()
332    
333     Returns the L<Net::XMPP2::Node> object for this stream error.
334    
335     =cut
336    
337     sub xml_node {
338     $_[0]->{node}
339     }
340    
341     =head3 condition ()
342    
343     Returns the error condition, which might be one of:
344    
345     aborted
346     incorrect-encoding
347     invalid-authzid
348     invalid-mechanism
349     mechanism-too-weak
350     not-authorized
351     temporary-auth-failure
352    
353     =cut
354    
355     sub condition {
356     $_[0]->{error_cond}
357     }
358    
359     sub string {
360     my ($self) = @_;
361    
362     sprintf "sasl error: %s",
363     $self->condition
364     }
365    
366 elmex 1.3 package Net::XMPP2::Error::Register;
367     our @ISA = qw/Net::XMPP2::Error::IQ/;
368    
369     =head1 SUBCLASS
370    
371     Net::XMPP2::Error::Register - In band registration error
372    
373     =cut
374    
375     =head2 METHODS
376    
377     =head3 register_state ()
378    
379     Returns the state of registration, one of:
380    
381     form-request
382     form-submitted
383    
384     =cut
385    
386     sub register_state {
387     my ($self) = @_;
388     $self->{register_state}
389     }
390    
391     sub string {
392     my ($self) = @_;
393    
394     sprintf "ibb registration error (in %s): %s",
395     $self->register_state,
396     $self->SUPER::string
397     }
398    
399 elmex 1.1 =head1 AUTHOR
400    
401     Robin Redeker, C<< <elmex at ta-sa.org> >>
402    
403     =head1 COPYRIGHT & LICENSE
404    
405     Copyright 2007 Robin Redeker, all rights reserved.
406    
407     This program is free software; you can redistribute it and/or modify it
408     under the same terms as Perl itself.
409    
410     =cut
411    
412     1; # End of Net::XMPP2