| 1 | package AnyEvent::XMPP::Error::Stanza; | ||
| 2 | 0 | ▶ | use AnyEvent::XMPP::Error; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 3 | use strict; | ||
| 4 | our @ISA = qw/AnyEvent::XMPP::Error/; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | AnyEvent::XMPP::Error::Stanza - Stanza errors | ||
| 9 | |||
| 10 | Subclass of L<AnyEvent::XMPP::Error> | ||
| 11 | |||
| 12 | =cut | ||
| 13 | |||
| 14 | sub init { | ||
| 15 | my ($self) = @_; | ||
| 16 | my $node = $self->xml_node; | ||
| 17 | |||
| 18 | unless (defined $node) { | ||
| 19 | $self->{error_cond} = 'client-timeout'; | ||
| 20 | $self->{error_type} = 'cancel'; | ||
| 21 | return; | ||
| 22 | } | ||
| 23 | |||
| 24 | my @error; | ||
| 25 | my ($err) = $node->find_all ([qw/client error/]); | ||
| 26 | |||
| 27 | unless ($err) { | ||
| 28 | warn "No error element found in error stanza!"; | ||
| 29 | $self->{text} = "Unknown Stanza error"; | ||
| 30 | return | ||
| 31 | } | ||
| 32 | |||
| 33 | $self->{error_type} = $err->attr ('type'); | ||
| 34 | $self->{error_code} = $err->attr ('code'); | ||
| 35 | |||
| 36 | if (my ($txt) = $err->find_all ([qw/stanzas text/])) { | ||
| 37 | $self->{error_text} = $txt->text; | ||
| 38 | } | ||
| 39 | |||
| 40 | for my $er ( | ||
| 41 | qw/bad-request conflict feature-not-implemented forbidden | ||
| 42 | gone internal-server-error item-not-found jid-malformed | ||
| 43 | not-acceptable not-allowed not-authorized payment-required | ||
| 44 | recipient-unavailable redirect registration-required | ||
| 45 | remote-server-not-found remote-server-timeout resource-constraint | ||
| 46 | service-unavailable subscription-required undefined-condition | ||
| 47 | unexpected-request/) | ||
| 48 | { | ||
| 49 | if (my ($el) = $err->find_all ([stanzas => $er])) { | ||
| 50 | $self->{error_cond} = $er; | ||
| 51 | $self->{error_cond_node} = $el; | ||
| 52 | last; | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | if (not ($self->{error_cond}) && defined $self->{error_code}) { | ||
| 57 | for my $er (keys %AnyEvent::XMPP::Writer::STANZA_ERRORS) { | ||
| 58 | my $ern = $AnyEvent::XMPP::Writer::STANZA_ERRORS{$er}; | ||
| 59 | if ($ern->[1] == $self->{error_code} && $ern->[0] eq $self->{error_type}) { | ||
| 60 | $self->{error_cond} = $er; | ||
| 61 | last; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | if (!(defined $self->{error_code}) && $self->{error_cond}) { | ||
| 67 | my $ern = $AnyEvent::XMPP::Writer::STANZA_ERRORS{$self->{error_cond}}; | ||
| 68 | $self->{error_type} = $ern->[0]; | ||
| 69 | $self->{error_code} = $ern->[1]; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | =head2 METHODS | ||
| 74 | |||
| 75 | =over 4 | ||
| 76 | |||
| 77 | =item B<xml_node ()> | ||
| 78 | |||
| 79 | Returns the L<AnyEvent::XMPP::Node> object for this Stanza error. | ||
| 80 | This method returns undef if the Stanza timeouted. | ||
| 81 | |||
| 82 | In the case of a timeout the C<condition> method returns C<client-timeout>, | ||
| 83 | C<type> returns 'cancel' and C<code> undef. | ||
| 84 | |||
| 85 | =cut | ||
| 86 | |||
| 87 | sub xml_node { | ||
| 88 | $_[0]->{node} | ||
| 89 | } | ||
| 90 | |||
| 91 | =item B<type ()> | ||
| 92 | |||
| 93 | This method returns one of: | ||
| 94 | |||
| 95 | 'cancel', 'continue', 'modify', 'auth' and 'wait' | ||
| 96 | |||
| 97 | =cut | ||
| 98 | |||
| 99 | sub type { $_[0]->{error_type} } | ||
| 100 | |||
| 101 | =item B<code ()> | ||
| 102 | |||
| 103 | This method returns the error code if one was found. | ||
| 104 | |||
| 105 | =cut | ||
| 106 | |||
| 107 | sub code { $_[0]->{error_code} } | ||
| 108 | |||
| 109 | =item B<condition ()> | ||
| 110 | |||
| 111 | Returns the error condition string if one was found when receiving the Stanza error. | ||
| 112 | It can be undef or one of: | ||
| 113 | |||
| 114 | bad-request | ||
| 115 | conflict | ||
| 116 | feature-not-implemented | ||
| 117 | forbidden | ||
| 118 | gone | ||
| 119 | internal-server-error | ||
| 120 | item-not-found | ||
| 121 | jid-malformed | ||
| 122 | not-acceptable | ||
| 123 | not-allowed | ||
| 124 | not-authorized | ||
| 125 | payment-required | ||
| 126 | recipient-unavailable | ||
| 127 | redirect | ||
| 128 | registration-required | ||
| 129 | remote-server-not-found | ||
| 130 | remote-server-timeout | ||
| 131 | resource-constraint | ||
| 132 | service-unavailable | ||
| 133 | subscription-required | ||
| 134 | undefined-condition | ||
| 135 | unexpected-request | ||
| 136 | |||
| 137 | |||
| 138 | =cut | ||
| 139 | |||
| 140 | sub condition { $_[0]->{error_cond} } | ||
| 141 | |||
| 142 | =item B<condition_node ()> | ||
| 143 | |||
| 144 | Returns the error condition node if one was found when receiving the Stanza error. | ||
| 145 | This is mostly for debugging purposes. | ||
| 146 | |||
| 147 | =cut | ||
| 148 | |||
| 149 | sub condition_node { $_[0]->{error_cond_node} } | ||
| 150 | |||
| 151 | =item B<text ()> | ||
| 152 | |||
| 153 | The humand readable error portion. Might be undef if none was received. | ||
| 154 | |||
| 155 | =cut | ||
| 156 | |||
| 157 | sub text { $_[0]->{error_text} } | ||
| 158 | |||
| 159 | sub string { | ||
| 160 | my ($self) = @_; | ||
| 161 | |||
| 162 | sprintf "stanza error: %s/%s (type %s): %s", | ||
| 163 | $self->code || '', | ||
| 164 | $self->condition || '', | ||
| 165 | $self->type, | ||
| 166 | $self->text | ||
| 167 | } | ||
| 168 | |||
| 169 | =back | ||
| 170 | |||
| 171 | =cut | ||
| 172 | |||
| 173 | |||
| 174 | =head1 AUTHOR | ||
| 175 | |||
| 176 | Robin Redeker, C<< <elmex at ta-sa.org> >>, JID: C<< <elmex at jabber.org> >> | ||
| 177 | |||
| 178 | =head1 COPYRIGHT & LICENSE | ||
| 179 | |||
| 180 | Copyright 2007, 2008 Robin Redeker, all rights reserved. | ||
| 181 | |||
| 182 | This program is free software; you can redistribute it and/or modify it | ||
| 183 | under the same terms as Perl itself. | ||
| 184 | |||
| 185 | =cut | ||
| 186 | |||
| 187 | 1; # End of AnyEvent::XMPP | ||