errors: make error types and conditions Literals
and set recommended defaults for type based on condition
This commit is contained in:
parent
1f934d375c
commit
b6f148e4e6
2 changed files with 64 additions and 3 deletions
|
@ -5,6 +5,10 @@
|
||||||
# :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
|
||||||
|
|
||||||
class XMPPError(Exception):
|
class XMPPError(Exception):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
@ -37,11 +41,15 @@ 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):
|
||||||
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.condition = condition
|
self.condition = condition
|
||||||
self.text = text
|
self.text = text
|
||||||
|
@ -110,3 +118,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",
|
||||||
|
}
|
|
@ -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 a new issue