diff options
author | Philipp Hörist <philipp@hoerist.com> | 2019-08-29 20:07:24 +0300 |
---|---|---|
committer | Philipp Hörist <philipp@hoerist.com> | 2019-08-29 20:35:21 +0300 |
commit | 0fdbbd1fedc662d3fca62ff81272aff8ca2200bd (patch) | |
tree | 7250ae8090f3b83b334c0170e6c3d0d4b46121aa /nbxmpp | |
parent | 193bbff1e71a38bcc88dbba485589677a86467fd (diff) |
Add StanzaMalformedError
Before we raised CommonError even if the stanza was not an error node.
Now we raise the dedicated StanzaMalformedError.
Diffstat (limited to 'nbxmpp')
-rw-r--r-- | nbxmpp/structs.py | 32 | ||||
-rw-r--r-- | nbxmpp/util.py | 18 |
2 files changed, 37 insertions, 13 deletions
diff --git a/nbxmpp/structs.py b/nbxmpp/structs.py index d86d48f..5c7358e 100644 --- a/nbxmpp/structs.py +++ b/nbxmpp/structs.py @@ -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__ = [] diff --git a/nbxmpp/util.py b/nbxmpp/util.py index 6170984..8ca4301 100644 --- a/nbxmpp/util.py +++ b/nbxmpp/util.py @@ -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 |