File: errors.rb

package info (click to toggle)
ruby-xmpp4r 0.5.6-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,384 kB
  • sloc: ruby: 17,382; xml: 74; sh: 12; makefile: 4
file content (284 lines) | stat: -rw-r--r-- 7,628 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# =XMPP4R - XMPP Library for Ruby
# License:: Ruby's license (see the LICENSE file) or GNU GPL, at your option.
# Website::http://xmpp4r.github.io

module Jabber

  # CUSTOM ERROR CLASSES

  # All of our custom errors are superclassed by JabberError < StandardError
  class JabberError < StandardError; end

  # A client-side only argument error
  class ArgumentError < JabberError; end

  # Server disconnected us
  class ServerDisconnected < JabberError; end
  
  ##
  # An error returned from the server
  #
  # e.g. This exception can be raised by helpers when they
  # receive a server reply with <tt>type='error'</tt>
  #
  # The ServerError carries a Jabber::ErrorResponse element
  #
  class ServerError < JabberError #:nodoc:

    ##
    # The error element which caused this exception
    attr_reader :error

    ##
    # Initialize a ServerError by passing an ErrorResponse instance
    # error:: [Error]
    def initialize(error)
      @error = error
    end

    ##
    # Sample output:
    # 'subscription-required: Please subscribe first'
    def to_s
      "#{@error.error}: #{@error.text}"
    end
  end

  class ClientAuthenticationFailure < JabberError; end

  class ComponentAuthenticationFailure < JabberError; end

  class NoNameXmlnsRegistered < JabberError
    def initialize(klass)
      super "Class #{klass} has not set name and xmlns"
    end
  end

  class SOCKS5Error < JabberError; end

  class InvalidChatState < JabberError; end

  ##
  # A class used to build/parse <error/> elements.
  # Look at XEP-0086 for explanation:
  # http://www.xmpp.org/extensions/xep-0086.html
  #
  # FIXME : XEP-0086 is officially deprecated.  What effect does that have on this class? Any?
  #
  class ErrorResponse < XMPPElement
    name_xmlns 'error'

    ##
    # errorcondition:: [nil] or [String] of the following:
    # * "bad-request"
    # * "conflict"
    # * "feature-not-implemented"
    # * "forbidden"
    # * "gone"
    # * "internal-server-error"
    # * "item-not-found"
    # * "jid-malformed"
    # * "not-acceptable"
    # * "not-allowed"
    # * "not-authorized"
    # * "payment-required"
    # * "recipient-unavailable"
    # * "redirect"
    # * "registration-required"
    # * "remote-server-not-found"
    # * "remote-server-timeout"
    # * "resource-constraint"
    # * "service-unavailable"
    # * "subscription-required"
    # * "undefined-condition"
    # * "unexpected-request"
    # Will raise an [Exception] if not [nil] and none of the above
    #
    # Also sets type and code to appropriate values according to errorcondition
    #
    # text: [nil] or [String] ErrorResponse text
    def initialize(errorcondition=nil, text=nil)
      if errorcondition.nil?
        super()
        set_text(text) unless text.nil?
      else
        errortype = nil
        errorcode = nil
        @@Errors.each { |cond,type,code|
          if errorcondition == cond
            errortype = type
            errorcode = code
          end
        }

        if errortype.nil? || errorcode.nil?
          raise ArgumentError, "Unknown error condition when initializing ErrorReponse"
        end

        super()
        set_error(errorcondition)
        set_type(errortype)
        set_code(errorcode)
        set_text(text) unless text.nil?
      end
    end

    ##
    # Get the 'Legacy error code' or nil
    # result:: [Integer] Error code
    def code
      if attributes['code']
        attributes['code'].to_i
      else
        nil
      end
    end

    ##
    # Set the 'Legacy error code' or nil
    # i:: [Integer] Error code
    def code=(i)
      if i.nil?
        attributes['code'] = nil
      else
        attributes['code'] = i.to_s
      end
    end

    ##
    # Set the 'Legacy error code' (chaining-friendly)
    def set_code(i)
      self.code = i
      self
    end

    ##
    # Get the 'XMPP error condition'
    #
    # This can be anything that possess the specific namespace,
    # checks don't apply here
    def error
      name = nil
      each_element { |e| name = e.name if (e.namespace == 'urn:ietf:params:xml:ns:xmpp-stanzas') && (e.name != 'text') }
      name
    end

    ##
    # Set the 'XMPP error condition'
    #
    # One previous element with that namespace will be deleted before
    #
    # s:: [String] Name of the element to be added,
    # namespace will be added automatically, checks don't apply here
    def error=(s)
      xe = nil
      each_element { |e| xe = e if (e.namespace == 'urn:ietf:params:xml:ns:xmpp-stanzas') && (e.name != 'text') }
      unless xe.nil?
        delete_element(xe)
      end

      add_element(s).add_namespace('urn:ietf:params:xml:ns:xmpp-stanzas')
    end

    ##
    # Set the 'XMPP error condition' (chaining-friendly)
    def set_error(s)
      self.error = s
      self
    end

    ##
    # Get the errors <text/> element text
    # result:: [String] or nil
    def text
      first_element_text('text') || super
    end

    ##
    # Set the errors <text/> element text
    # (Previous <text/> elements will be deleted first)
    # s:: [String] <text/> content or [nil] if no <text/> element
    def text=(s)
      delete_elements('text')

      unless s.nil?
        e = add_element('text')
        e.add_namespace('urn:ietf:params:xml:ns:xmpp-stanzas')
        e.text = s
      end
    end

    ##
    # Set the errors <text/> element text (chaining-friendly)
    def set_text(s)
      self.text = s
      self
    end

    ##
    # Get the type of error
    # (meaning how to proceed)
    # result:: [Symbol] or [nil] as following:
    # * :auth
    # * :cancel
    # * :continue
    # * :modify
    # * :wait
    def type
      case attributes['type']
        when 'auth' then :auth
        when 'cancel' then :cancel
        when 'continue' then :continue
        when 'modify' then :modify
        when 'wait' then :wait
        else nil
      end
    end

    ##
    # Set the type of error (see ErrorResponse#type)
    def type=(t)
      case t
        when :auth then attributes['type'] = 'auth'
        when :cancel then attributes['type'] = 'cancel'
        when :continue then attributes['type'] = 'continue'
        when :modify then attributes['type'] = 'modify'
        when :wait then attributes['type'] = 'wait'
        else attributes['type'] = nil
      end
    end

    ##
    # Set the type of error (chaining-friendly)
    def set_type(t)
      self.type = t
      self
    end

    ##
    # Possible XMPP error conditions, types and codes (XEP-0086)
    @@Errors = [['bad-request', :modify, 400],
                ['conflict', :cancel, 409],
                ['feature-not-implemented', :cancel, 501],
                ['forbidden', :auth, 403],
                ['gone', :modify, 302],
                ['internal-server-error', :wait, 500],
                ['item-not-found', :cancel, 404],
                ['jid-malformed', :modify, 400],
                ['not-acceptable', :modify, 406],
                ['not-allowed', :cancel, 405],
                ['not-authorized', :auth, 401],
                ['payment-required', :auth, 402],
                ['recipient-unavailable', :wait, 404],
                ['redirect', :modify, 302],
                ['registration-required', :auth, 407],
                ['remote-server-not-found', :cancel, 404],
                ['remote-server-timeout', :wait, 504],
                ['resource-constraint', :wait, 500],
                ['service-unavailable', :cancel, 503],
                ['subscription-required', :auth, 407],
                ['undefined-condition', nil, 500],
                ['unexpected-request', :wait, 400]]
  end

end