From b6f148e4e604a7a49149df2388ce7d7a945f7848 Mon Sep 17 00:00:00 2001 From: nicoco Date: Thu, 23 Feb 2023 06:58:37 +0100 Subject: [PATCH] errors: make error types and conditions Literals and set recommended defaults for type based on condition --- slixmpp/exceptions.py | 38 ++++++++++++++++++++++++++++++++++++-- slixmpp/types.py | 29 ++++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/slixmpp/exceptions.py b/slixmpp/exceptions.py index 0df79a00..61c6fa3c 100644 --- a/slixmpp/exceptions.py +++ b/slixmpp/exceptions.py @@ -5,6 +5,10 @@ # :copyright: (c) 2011 Nathanael C. Fritz # :license: MIT, see LICENSE for more details +from typing import Optional + +from .types import ErrorConditions, ErrorTypes + class XMPPError(Exception): """ @@ -37,11 +41,15 @@ class XMPPError(Exception): Defaults to ``True``. """ - def __init__(self, condition='undefined-condition', text='', - etype='cancel', extension=None, extension_ns=None, + def __init__(self, condition: ErrorConditions='undefined-condition', text='', + etype: Optional[ErrorTypes]=None, extension=None, extension_ns=None, extension_args=None, clear=True): 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.condition = condition self.text = text @@ -110,3 +118,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", +} \ No newline at end of file diff --git a/slixmpp/types.py b/slixmpp/types.py index 99492062..f6a5f5d2 100644 --- a/slixmpp/types.py +++ b/slixmpp/types.py @@ -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' ]