PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/xmpp4r-0.3.2/lib/xmpp4r/error.rb

https://github.com/koke/github-services
Ruby | 225 lines | 124 code | 19 blank | 82 comment | 18 complexity | 2a36bb3c3caa0cfd8df74348070b04e6 MD5 | raw file
Possible License(s): GPL-2.0
  1. # =XMPP4R - XMPP Library for Ruby
  2. # License:: Ruby's license (see the LICENSE file) or GNU GPL, at your option.
  3. # Website::http://home.gna.org/xmpp4r/
  4. module Jabber
  5. ##
  6. # A class used to build/parse <error/> elements.
  7. # Look at JEP 0086 for explanation.
  8. class Error < XMPPElement
  9. name_xmlns 'error'
  10. ##
  11. # errorcondition:: [nil] or [String] of the following:
  12. # * "bad-request"
  13. # * "conflict"
  14. # * "feature-not-implemented"
  15. # * "forbidden"
  16. # * "gone"
  17. # * "internal-server-error"
  18. # * "item-not-found"
  19. # * "jid-malformed"
  20. # * "not-acceptable"
  21. # * "not-allowed"
  22. # * "not-authorized"
  23. # * "payment-required"
  24. # * "recipient-unavailable"
  25. # * "redirect"
  26. # * "registration-required"
  27. # * "remote-server-not-found"
  28. # * "remote-server-timeout"
  29. # * "resource-constraint"
  30. # * "service-unavailable"
  31. # * "subscription-required"
  32. # * "undefined-condition"
  33. # * "unexpected-request"
  34. # Will raise an [Exception] if not [nil] and none of the above
  35. #
  36. # Does also set type and code to appropriate values according to errorcondition
  37. #
  38. # text: [nil] or [String] Error text
  39. def initialize(errorcondition=nil, text=nil)
  40. if errorcondition.nil?
  41. super()
  42. set_text(text) unless text.nil?
  43. else
  44. errortype = nil
  45. errorcode = nil
  46. @@Errors.each { |cond,type,code|
  47. if errorcondition == cond
  48. errortype = type
  49. errorcode = code
  50. end
  51. }
  52. if errortype.nil? || errorcode.nil?
  53. raise("Unknown error condition when initializing Error")
  54. end
  55. super()
  56. set_error(errorcondition)
  57. set_type(errortype)
  58. set_code(errorcode)
  59. set_text(text) unless text.nil?
  60. end
  61. end
  62. ##
  63. # Get the 'Legacy error code' or nil
  64. # result:: [Integer] Error code
  65. def code
  66. if attributes['code']
  67. attributes['code'].to_i
  68. else
  69. nil
  70. end
  71. end
  72. ##
  73. # Set the 'Legacy error code' or nil
  74. # i:: [Integer] Error code
  75. def code=(i)
  76. if i.nil?
  77. attributes['code'] = nil
  78. else
  79. attributes['code'] = i.to_s
  80. end
  81. end
  82. ##
  83. # Set the 'Legacy error code' (chaining-friendly)
  84. def set_code(i)
  85. self.code = i
  86. self
  87. end
  88. ##
  89. # Get the 'XMPP error condition'
  90. #
  91. # This can be anything that possess the specific namespace,
  92. # checks don't apply here
  93. def error
  94. name = nil
  95. each_element { |e| name = e.name if (e.namespace == 'urn:ietf:params:xml:ns:xmpp-stanzas') && (e.name != 'text') }
  96. name
  97. end
  98. ##
  99. # Set the 'XMPP error condition'
  100. #
  101. # One previous element with that namespace will be deleted before
  102. #
  103. # s:: [String] Name of the element to be added,
  104. # namespace will be added automatically, checks don't apply here
  105. def error=(s)
  106. xe = nil
  107. each_element { |e| xe = e if (e.namespace == 'urn:ietf:params:xml:ns:xmpp-stanzas') && (e.name != 'text') }
  108. unless xe.nil?
  109. delete_element(xe)
  110. end
  111. add_element(s).add_namespace('urn:ietf:params:xml:ns:xmpp-stanzas')
  112. end
  113. ##
  114. # Set the 'XMPP error condition' (chaining-friendly)
  115. def set_error(s)
  116. self.error = s
  117. self
  118. end
  119. ##
  120. # Get the errors <text/> element text
  121. # result:: [String] or nil
  122. def text
  123. first_element_text('text') || super
  124. end
  125. ##
  126. # Set the errors <text/> element text
  127. # (Previous <text/> elements will be deleted first)
  128. # s:: [String] <text/> content or [nil] if no <text/> element
  129. def text=(s)
  130. delete_elements('text')
  131. unless s.nil?
  132. e = add_element('text')
  133. e.add_namespace('urn:ietf:params:xml:ns:xmpp-stanzas')
  134. e.text = s
  135. end
  136. end
  137. ##
  138. # Set the errors <text/> element text (chaining-friendly)
  139. def set_text(s)
  140. self.text = s
  141. self
  142. end
  143. ##
  144. # Get the type of error
  145. # (meaning how to proceed)
  146. # result:: [Symbol] or [nil] as following:
  147. # * :auth
  148. # * :cancel
  149. # * :continue
  150. # * :modify
  151. # * :wait
  152. def type
  153. case attributes['type']
  154. when 'auth' then :auth
  155. when 'cancel' then :cancel
  156. when 'continue' then :continue
  157. when 'modify' then :modify
  158. when 'wait' then :wait
  159. else nil
  160. end
  161. end
  162. ##
  163. # Set the type of error (see Error#type)
  164. def type=(t)
  165. case t
  166. when :auth then attributes['type'] = 'auth'
  167. when :cancel then attributes['type'] = 'cancel'
  168. when :continue then attributes['type'] = 'continue'
  169. when :modify then attributes['type'] = 'modify'
  170. when :wait then attributes['type'] = 'wait'
  171. else attributes['type'] = nil
  172. end
  173. end
  174. ##
  175. # Set the type of error (chaining-friendly)
  176. def set_type(t)
  177. self.type = t
  178. self
  179. end
  180. ##
  181. # Possible XMPP error conditions, types and codes
  182. # (JEP 0086)
  183. @@Errors = [['bad-request', :modify, 400],
  184. ['conflict', :cancel, 409],
  185. ['feature-not-implemented', :cancel, 501],
  186. ['forbidden', :auth, 403],
  187. ['gone', :modify, 302],
  188. ['internal-server-error', :wait, 500],
  189. ['item-not-found', :cancel, 404],
  190. ['jid-malformed', :modify, 400],
  191. ['not-acceptable', :modify, 406],
  192. ['not-allowed', :cancel, 405],
  193. ['not-authorized', :auth, 401],
  194. ['payment-required', :auth, 402],
  195. ['recipient-unavailable', :wait, 404],
  196. ['redirect', :modify, 302],
  197. ['registration-required', :auth, 407],
  198. ['remote-server-not-found', :cancel, 404],
  199. ['remote-server-timeout', :wait, 504],
  200. ['resource-constraint', :wait, 500],
  201. ['service-unavailable', :cancel, 503],
  202. ['subscription-required', :auth, 407],
  203. ['undefined-condition', nil, 500],
  204. ['unexpected-request', :wait, 400]]
  205. end
  206. end