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



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

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.



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

def extras
  @extras
end

#nameSymbol (readonly)

The error name

Returns:

  • (Symbol)


67
68
69
# File 'lib/blather/errors/stanza_error.rb', line 67

def name
  @name
end

#originalObject (readonly)

Returns the value of attribute original.



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

def original
  @original
end

#textObject (readonly)

Returns the value of attribute text.



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

def text
  @text
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

Class Method Details

.import(node) ⇒ Blather::StanzaError

Factory method for instantiating the proper class for the error

Parameters:

Returns:



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

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



100
101
102
# File 'lib/blather/errors/stanza_error.rb', line 100

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

#to_nodeBlather::XMPPNode

Creates an XML node from the error

Returns:



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

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_xmlString

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

Returns:

  • (String)


95
96
97
# File 'lib/blather/errors/stanza_error.rb', line 95

def to_xml
  to_node.to_s
end