Merge branch 'better-errors' into 'master'
Improve errors See merge request poezio/slixmpp!230
This commit is contained in:
commit
ebb8bd1e71
@ -5,6 +5,11 @@
|
|||||||
# :copyright: (c) 2011 Nathanael C. Fritz
|
# :copyright: (c) 2011 Nathanael C. Fritz
|
||||||
# :license: MIT, see LICENSE for more details
|
# :license: MIT, see LICENSE for more details
|
||||||
|
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from .types import ErrorConditions, ErrorTypes, JidStr
|
||||||
|
|
||||||
|
|
||||||
class XMPPError(Exception):
|
class XMPPError(Exception):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@ -37,12 +42,17 @@ class XMPPError(Exception):
|
|||||||
Defaults to ``True``.
|
Defaults to ``True``.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, condition='undefined-condition', text='',
|
def __init__(self, condition: ErrorConditions='undefined-condition', text='',
|
||||||
etype='cancel', extension=None, extension_ns=None,
|
etype: Optional[ErrorTypes]=None, extension=None, extension_ns=None,
|
||||||
extension_args=None, clear=True):
|
extension_args=None, clear=True, by: Optional[JidStr] = None):
|
||||||
if extension_args is None:
|
if extension_args is None:
|
||||||
extension_args = {}
|
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.condition = condition
|
||||||
self.text = text
|
self.text = text
|
||||||
self.etype = etype
|
self.etype = etype
|
||||||
@ -110,3 +120,29 @@ class PresenceError(XMPPError):
|
|||||||
etype=pres['error']['type'],
|
etype=pres['error']['type'],
|
||||||
)
|
)
|
||||||
self.presence = pres
|
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",
|
||||||
|
}
|
@ -176,7 +176,7 @@ class Message(RootStanza):
|
|||||||
"""
|
"""
|
||||||
new_message = StanzaBase.reply(self, clear)
|
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['to'] = new_message['to'].bare
|
||||||
|
|
||||||
new_message['thread'] = self['thread']
|
new_message['thread'] = self['thread']
|
||||||
|
@ -63,6 +63,8 @@ class RootStanza(StanzaBase):
|
|||||||
reply['error']['condition'] = e.condition
|
reply['error']['condition'] = e.condition
|
||||||
reply['error']['text'] = e.text
|
reply['error']['text'] = e.text
|
||||||
reply['error']['type'] = e.etype
|
reply['error']['type'] = e.etype
|
||||||
|
if e.by:
|
||||||
|
reply["error"]["by"] = e.by
|
||||||
if e.extension is not None:
|
if e.extension is not None:
|
||||||
# Extended error tag
|
# Extended error tag
|
||||||
extxml = ET.Element("{%s}%s" % (e.extension_ns, e.extension),
|
extxml = ET.Element("{%s}%s" % (e.extension_ns, e.extension),
|
||||||
|
@ -83,8 +83,35 @@ MAMDefault = Literal['always', 'never', 'roster']
|
|||||||
|
|
||||||
FilterString = Literal['in', 'out', 'out_sync']
|
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__ = [
|
__all__ = [
|
||||||
'Protocol', 'TypedDict', 'Literal', 'OptJid', 'OptJidStr', 'JidStr', 'MAMDefault',
|
'Protocol', 'TypedDict', 'Literal', 'OptJid', 'OptJidStr', 'JidStr', 'MAMDefault',
|
||||||
'PresenceTypes', 'PresenceShows', 'MessageTypes', 'IqTypes', 'MucRole',
|
'PresenceTypes', 'PresenceShows', 'MessageTypes', 'IqTypes', 'MucRole',
|
||||||
'MucAffiliation', 'FilterString',
|
'MucAffiliation', 'FilterString', 'ErrorConditions', 'ErrorTypes'
|
||||||
]
|
]
|
||||||
|
Loading…
Reference in New Issue
Block a user