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].freeze

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

Create a new StanzaError

[RFC3920](xmpp.org/rfcs/rfc3920.html#rfc.section.9.3.2)

Parameters:

  • original (Blather::XMPPNode)

    the original stanza

  • name (String)

    the error name

  • type (#to_s)

    the error type as specified in

  • text (String, nil) (defaults to: nil)

    additional text for the error

  • extras (Array<Blather::XMPPNode>) (defaults to: [])

    an array of extra nodes to add



45
46
47
48
49
50
51
# File 'lib/blather/errors/stanza_error.rb', line 45

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.



15
16
17
# File 'lib/blather/errors/stanza_error.rb', line 15

def extras
  @extras
end

#nameSymbol (readonly)

The error name

Returns:

  • (Symbol)


69
70
71
# File 'lib/blather/errors/stanza_error.rb', line 69

def name
  @name
end

#originalObject (readonly)

Returns the value of attribute original.



15
16
17
# File 'lib/blather/errors/stanza_error.rb', line 15

def original
  @original
end

#textObject (readonly)

Returns the value of attribute text.



15
16
17
# File 'lib/blather/errors/stanza_error.rb', line 15

def text
  @text
end

#typeObject

Returns the value of attribute type.



15
16
17
# File 'lib/blather/errors/stanza_error.rb', line 15

def type
  @type
end

Class Method Details

.import(node) ⇒ Blather::StanzaError

Factory method for instantiating the proper class for the error

Parameters:

Returns:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/blather/errors/stanza_error.rb', line 21

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



103
104
105
# File 'lib/blather/errors/stanza_error.rb', line 103

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

#to_nodeBlather::XMPPNode

Creates an XML node from the error

Returns:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/blather/errors/stanza_error.rb', line 76

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))
  error_node['type'] = self.type
  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_xml(*args) ⇒ String

Convert the object to a proper node then convert it to a string

Returns:

  • (String)


98
99
100
# File 'lib/blather/errors/stanza_error.rb', line 98

def to_xml(*args)
  to_node.to_xml(*args)
end