diff options
author | Philipp Hörist <philipp@hoerist.com> | 2019-08-05 11:58:16 +0300 |
---|---|---|
committer | Philipp Hörist <philipp@hoerist.com> | 2019-08-05 11:58:16 +0300 |
commit | d1e511bfae54fb1f67c6167876436ad7d87d5fef (patch) | |
tree | 4a7973f16d8762676dd4f26d11da1800245cd1df /nbxmpp/structs.py | |
parent | 30f65e2197f6c5c20e04a43a69b12071c935759e (diff) |
Refactor CommonError
- Parse application errors
- Parse text elements in all languages
Diffstat (limited to 'nbxmpp/structs.py')
-rw-r--r-- | nbxmpp/structs.py | 49 |
1 files changed, 45 insertions, 4 deletions
diff --git a/nbxmpp/structs.py b/nbxmpp/structs.py index acf2bab..f6a22cc 100644 --- a/nbxmpp/structs.py +++ b/nbxmpp/structs.py @@ -309,11 +309,52 @@ class OMEMOBundle(namedtuple('OMEMOBundle', 'spk spk_signature ik otpks')): return random.SystemRandom().choice(self.otpks) -class CommonError(namedtuple('CommonError', 'type message jid')): +class CommonError: + def __init__(self, stanza): + self._error_node = stanza.getTag('error') + self.condition = stanza.getError() + self.app_condition = stanza.getAppError() + self.type = stanza.getErrorType() + self.jid = stanza.getFrom() + 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 + + def get_text(self, pref_lang=None): + if pref_lang is not None: + text = self._text.get(pref_lang) + if text is not None: + return text + + if self._text: + text = self._text.get('en') + if text is not None: + return text + + text = self._text.get(None) + if text is not None: + return text + return self._text.popitem()[1] + return '' + + def set_text(self, lang, text): + self._text[lang] = text + def __str__(self): - if self.message is not None: - return 'Error from %s %s: %s' % (self.jid, self.type, self.message) - return 'Error from %s: %s' % (self.jid, self.type) + condition = self.condition + if self.app_condition is not None: + condition = '%s (%s)' % (self.condition, self.app_condition) + text = self.get_text('en') or '' + if text: + text = ' - %s' % text + return 'Error from %s: %s%s' % (self.jid, condition, text) class TuneData(namedtuple('TuneData', 'artist length rating source title track uri')): |