Commit 0fdbbd1f authored by Philipp Hörist's avatar Philipp Hörist

Add StanzaMalformedError

Before we raised CommonError even if the stanza was not an error node.
Now we raise the dedicated StanzaMalformedError.
parent 193bbff1
......@@ -368,13 +368,11 @@ class CommonError:
self.id = stanza.getID()
self._text = {}
error = stanza.getTag('error')
if error is not None:
text_elements = error.getTags('text', namespace=NS_STANZAS)
for element in text_elements:
lang = element.getXmlLang()
text = element.getData()
self._text[lang] = text
text_elements = self._error_node.getTags('text', namespace=NS_STANZAS)
for element in text_elements:
lang = element.getXmlLang()
text = element.getData()
self._text[lang] = text
def get_text(self, pref_lang=None):
if pref_lang is not None:
......@@ -406,6 +404,26 @@ class CommonError:
return 'Error from %s: %s%s' % (self.jid, condition, text)
class StanzaMalformedError(CommonError):
def __init__(self, stanza, text):
self._error_node = None
self.condition = 'stanza-malformed'
self.condition_data = None
self.app_condition = None
self.type = None
self.jid = stanza.getFrom()
self.id = stanza.getID()
self._text = {}
if text:
self._text['en'] = text
def __str__(self):
text = self.get_text('en')
if text:
text = ': %s' % text
return 'Received malformed stanza from %s%s' % (self.jid, text)
class TuneData(namedtuple('TuneData', 'artist length rating source title track uri')):
__slots__ = []
......
......@@ -28,12 +28,14 @@ import precis_i18n.codec
from nbxmpp.protocol import JID
from nbxmpp.protocol import InvalidJid
from nbxmpp.protocol import DiscoInfoMalformed
from nbxmpp.protocol import isErrorNode
from nbxmpp.stringprepare import nameprep
from nbxmpp.structs import Properties
from nbxmpp.structs import IqProperties
from nbxmpp.structs import MessageProperties
from nbxmpp.structs import PresenceProperties
from nbxmpp.structs import CommonError
from nbxmpp.structs import StanzaMalformedError
from nbxmpp.third_party.hsluv import hsluv_to_rgb
log = logging.getLogger('nbxmpp.util')
......@@ -140,18 +142,22 @@ def to_xs_boolean(value):
error_classes = {}
def error_factory(stanza):
def error_factory(stanza, condition=None, text=None):
if condition == 'stanza-malformed':
return StanzaMalformedError(stanza, text)
app_namespace = stanza.getAppErrorNamespace()
return error_classes.get(app_namespace, CommonError)(stanza)
def raise_error(log_method, stanza, condition=None, text=None):
error = error_factory(stanza)
if text is not None:
error.set_text('en', str(text))
if condition is not None:
error.condition = str(condition)
if not isErrorNode(stanza) and condition != 'stanza-malformed':
condition = 'stanza-malformed'
if log_method.__name__ not in ('warning', 'error'):
log_method = log_method.__self__.warning
error = error_factory(stanza, condition, text)
log_method(error)
if log_method.__name__ in ('warning', 'error'):
log_method(stanza)
return error
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment