Exception: Blather::StanzaError

Inherits:
BlatherError show all
Defined in:
lib/blather/errors/stanza_error.rb

Overview

Stanza errors RFC3920 Section 9.3 (xmpp.org/rfcs/rfc3920.html#stanzas-error)

Constant Summary collapse

STANZA_ERR_NS =
'urn:ietf:params:xml:ns:xmpp-stanzas'
VALID_TYPES =
[:cancel, :continue, :modify, :auth, :wait]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BlatherError

handler_list, #id, register

Constructor Details

#initialize(original, name, type, text = nil, extras = []) ⇒ StanzaError

original An original node must be provided for stanza errors. You can’t declare a stanza error on without a stanza. type is the error type specified in RFC3920 (xmpp.org/rfcs/rfc3920.html#rfc.section.9.3.2) text is an option error description extras an array of application specific nodes to add to the error. These should be properly namespaced.



39
40
41
42
43
44
45
# File 'lib/blather/errors/stanza_error.rb', line 39

def initialize(original, name, type, text = nil, extras = [])
  @original = original
  @name = name
  self.type = type
  @text = text
  @extras = extras
end

Instance Attribute Details

#extrasObject (readonly)

Returns the value of attribute extras.



12
13
14
# File 'lib/blather/errors/stanza_error.rb', line 12

def extras
  @extras
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/blather/errors/stanza_error.rb', line 12

def name
  @name
end

#originalObject (readonly)

Returns the value of attribute original.



12
13
14
# File 'lib/blather/errors/stanza_error.rb', line 12

def original
  @original
end

#textObject (readonly)

Returns the value of attribute text.



12
13
14
# File 'lib/blather/errors/stanza_error.rb', line 12

def text
  @text
end

#typeObject

Returns the value of attribute type.



12
13
14
# File 'lib/blather/errors/stanza_error.rb', line 12

def type
  @type
end

Class Method Details

.import(node) ⇒ Object

Factory method for instantiating the proper class for the error



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/blather/errors/stanza_error.rb', line 17

def self.import(node)
  original = node.copy
  original.remove_child 'error'

  error_node = node.find_first '//*[local-name()="error"]'

  name = error_node.find_first('child::*[name()!="text"]', STANZA_ERR_NS).element_name
  type = error_node['type']
  text = node.find_first 'descendant::*[name()="text"]', STANZA_ERR_NS
  text = text.content if text

  extras = error_node.find("descendant::*[name()!='text' and name()!='#{name}']").map { |n| n }

  self.new original, name, type, text, extras
end

Instance Method Details

#inspectObject Also known as: to_s

:nodoc:



85
86
87
# File 'lib/blather/errors/stanza_error.rb', line 85

def inspect # :nodoc:
  "Stanza Error (#{@name}): #{self.text} [#{self.extras}]"
end

#to_nodeObject

Creates an XML node from the error



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/blather/errors/stanza_error.rb', line 61

def to_node
  node = self.original.reply
  node.type = 'error'
  node << (error_node = XMPPNode.new('error'))

  error_node << (err = XMPPNode.new(@name, error_node.document))
  err.namespace = 'urn:ietf:params:xml:ns:xmpp-stanzas'

  if self.text
    error_node << (text = XMPPNode.new('text', error_node.document))
    text.namespace = 'urn:ietf:params:xml:ns:xmpp-stanzas'
    text.content = self.text
  end

  self.extras.each { |extra| error_node << extra.dup }
  node
end

#to_xmlObject

Turns the object into XML fit to be sent over the stream



81
82
83
# File 'lib/blather/errors/stanza_error.rb', line 81

def to_xml
  to_node.to_s
end