Merge branch 'better-errors' into 'master'

Improve errors

See merge request poezio/slixmpp!230
This commit is contained in:
Maxime Buquet 2023-02-24 14:41:09 +00:00
commit ebb8bd1e71
4 changed files with 70 additions and 5 deletions

View File

@ -5,6 +5,11 @@
# :copyright: (c) 2011 Nathanael C. Fritz
# :license: MIT, see LICENSE for more details
from typing import Optional
from .types import ErrorConditions, ErrorTypes, JidStr
class XMPPError(Exception):
"""
@ -37,12 +42,17 @@ class XMPPError(Exception):
Defaults to ``True``.
"""
def __init__(self, condition='undefined-condition', text='',
etype='cancel', extension=None, extension_ns=None,
extension_args=None, clear=True):
def __init__(self, condition: ErrorConditions='undefined-condition', text='',
etype: Optional[ErrorTypes]=None, extension=None, extension_ns=None,
extension_args=None, clear=True, by: Optional[JidStr] = None):
if extension_args is None:
extension_args = {}
if condition not in _DEFAULT_ERROR_TYPES:
raise ValueError("This is not a valid condition type", condition)
if etype is None:
etype = _DEFAULT_ERROR_TYPES[condition]
self.by = by
self.condition = condition
self.text = text
self.etype = etype
@ -110,3 +120,29 @@ class PresenceError(XMPPError):
etype=pres['error']['type'],
)
self.presence = pres
_DEFAULT_ERROR_TYPES: dict[ErrorConditions, ErrorTypes] = {
"bad-request": "modify",
"conflict": "cancel",
"feature-not-implemented": "cancel",
"forbidden": "auth",
"gone": "modify",
"internal-server-error": "wait",
"item-not-found": "cancel",
"jid-malformed": "modify",
"not-acceptable": "modify",
"not-allowed": "cancel",
"not-authorized": "auth",
"payment-required": "auth",
"recipient-unavailable": "wait",
"redirect": "modify",
"registration-required": "auth",
"remote-server-not-found": "cancel",
"remote-server-timeout": "wait",
"resource-constraint": "wait",
"service-unavailable": "cancel",
"subscription-required": "auth",
"undefined-condition": "cancel",
"unexpected-request": "modify",
}

View File

@ -176,7 +176,7 @@ class Message(RootStanza):
"""
new_message = StanzaBase.reply(self, clear)
if self['type'] == 'groupchat':
if not getattr(self.stream, "is_component", False) and self['type'] == 'groupchat':
new_message['to'] = new_message['to'].bare
new_message['thread'] = self['thread']

View File

@ -63,6 +63,8 @@ class RootStanza(StanzaBase):
reply['error']['condition'] = e.condition
reply['error']['text'] = e.text
reply['error']['type'] = e.etype
if e.by:
reply["error"]["by"] = e.by
if e.extension is not None:
# Extended error tag
extxml = ET.Element("{%s}%s" % (e.extension_ns, e.extension),

View File

@ -83,8 +83,35 @@ MAMDefault = Literal['always', 'never', 'roster']
FilterString = Literal['in', 'out', 'out_sync']
ErrorTypes = Literal["modify", "cancel", "auth", "wait", "cancel"]
ErrorConditions = Literal[
"bad-request",
"conflict",
"feature-not-implemented",
"forbidden",
"gone",
"internal-server-error",
"item-not-found",
"jid-malformed",
"not-acceptable",
"not-allowed",
"not-authorized",
"payment-required",
"recipient-unavailable",
"redirect",
"registration-required",
"remote-server-not-found",
"remote-server-timeout",
"resource-constraint",
"service-unavailable",
"subscription-required",
"undefined-condition",
"unexpected-request",
]
__all__ = [
'Protocol', 'TypedDict', 'Literal', 'OptJid', 'OptJidStr', 'JidStr', 'MAMDefault',
'PresenceTypes', 'PresenceShows', 'MessageTypes', 'IqTypes', 'MucRole',
'MucAffiliation', 'FilterString',
'MucAffiliation', 'FilterString', 'ErrorConditions', 'ErrorTypes'
]