From 08f48b0c8be7535754ba9494d13276f69485bd44 Mon Sep 17 00:00:00 2001 From: mathieui Date: Sat, 12 Dec 2020 22:51:34 +0100 Subject: Move get_error_message to common --- poezio/common.py | 72 +++++++++++++++++++++++++++++++++++++++++++++++++ poezio/core/core.py | 41 ++-------------------------- poezio/core/handlers.py | 8 +++--- poezio/core/structs.py | 35 +----------------------- 4 files changed, 79 insertions(+), 77 deletions(-) (limited to 'poezio') diff --git a/poezio/common.py b/poezio/common.py index 5332d408..9f218160 100644 --- a/poezio/common.py +++ b/poezio/common.py @@ -505,3 +505,75 @@ def to_utc(time: datetime) -> datetime: time = time.replace(tzinfo=tzone).astimezone(tz=timezone.utc) # Return an offset-naive datetime return time.replace(tzinfo=None) + + +# http://xmpp.org/extensions/xep-0045.html#errorstatus +ERROR_AND_STATUS_CODES = { + '401': 'A password is required', + '403': 'Permission denied', + '404': 'The room doesn’t exist', + '405': 'Your are not allowed to create a new room', + '406': 'A reserved nick must be used', + '407': 'You are not in the member list', + '409': 'This nickname is already in use or has been reserved', + '503': 'The maximum number of users has been reached', +} + + +# http://xmpp.org/extensions/xep-0086.html +DEPRECATED_ERRORS = { + '302': 'Redirect', + '400': 'Bad request', + '401': 'Not authorized', + '402': 'Payment required', + '403': 'Forbidden', + '404': 'Not found', + '405': 'Not allowed', + '406': 'Not acceptable', + '407': 'Registration required', + '408': 'Request timeout', + '409': 'Conflict', + '500': 'Internal server error', + '501': 'Feature not implemented', + '502': 'Remote server error', + '503': 'Service unavailable', + '504': 'Remote server timeout', + '510': 'Disconnected', +} + + +def get_error_message(stanza: Message, deprecated: bool = False) -> str: + """ + Takes a stanza of the form + and return a well formed string containing error information + """ + sender = stanza['from'] + msg = stanza['error']['type'] + condition = stanza['error']['condition'] + code = stanza['error']['code'] + body = stanza['error']['text'] + if not body: + if deprecated: + if code in DEPRECATED_ERRORS: + body = DEPRECATED_ERRORS[code] + else: + body = condition or 'Unknown error' + else: + if code in ERROR_AND_STATUS_CODES: + body = ERROR_AND_STATUS_CODES[code] + else: + body = condition or 'Unknown error' + if code: + message = '%(from)s: %(code)s - %(msg)s: %(body)s' % { + 'from': sender, + 'msg': msg, + 'body': body, + 'code': code + } + else: + message = '%(from)s: %(msg)s: %(body)s' % { + 'from': sender, + 'msg': msg, + 'body': body + } + return message diff --git a/poezio/core/core.py b/poezio/core/core.py index 2151600f..c79750de 100644 --- a/poezio/core/core.py +++ b/poezio/core/core.py @@ -45,6 +45,7 @@ from poezio import timed_events from poezio import windows from poezio.bookmarks import BookmarkList +from poezio.common import get_error_message from poezio.config import config, firstrun from poezio.contact import Contact, Resource from poezio.daemon import Executor @@ -66,8 +67,6 @@ from poezio.core.handlers import HandlerCore from poezio.core.structs import ( Command, Status, - DEPRECATED_ERRORS, - ERROR_AND_STATUS_CODES, POSSIBLE_SHOW, ) @@ -989,42 +988,6 @@ class Core: for jid in jids: self.invite(jid, room) - def get_error_message(self, stanza, deprecated: bool = False): - """ - Takes a stanza of the form - and return a well formed string containing error information - """ - sender = stanza['from'] - msg = stanza['error']['type'] - condition = stanza['error']['condition'] - code = stanza['error']['code'] - body = stanza['error']['text'] - if not body: - if deprecated: - if code in DEPRECATED_ERRORS: - body = DEPRECATED_ERRORS[code] - else: - body = condition or 'Unknown error' - else: - if code in ERROR_AND_STATUS_CODES: - body = ERROR_AND_STATUS_CODES[code] - else: - body = condition or 'Unknown error' - if code: - message = '%(from)s: %(code)s - %(msg)s: %(body)s' % { - 'from': sender, - 'msg': msg, - 'body': body, - 'code': code - } - else: - message = '%(from)s: %(msg)s: %(body)s' % { - 'from': sender, - 'msg': msg, - 'body': body - } - return message - ####################### Tab logic-related things ############################## ### Tab getters ### @@ -2125,7 +2088,7 @@ class Core: tab = self.tabs.by_name_and_class(room_name, tabs.MucTab) if not tab: return - error_message = self.get_error_message(error) + error_message = get_error_message(error) tab.add_message( Message( error_message, diff --git a/poezio/core/handlers.py b/poezio/core/handlers.py index 1fffaec1..a388c5d6 100644 --- a/poezio/core/handlers.py +++ b/poezio/core/handlers.py @@ -31,7 +31,7 @@ from poezio import pep from poezio import tabs from poezio import xhtml from poezio import multiuserchat as muc -from poezio.common import safeJID +from poezio.common import safeJID, get_error_message from poezio.config import config, get_image_cache from poezio.core.structs import Status from poezio.contact import Resource @@ -304,7 +304,7 @@ class HandlerCore: if jid_from.full == jid_from.bare: self.core.room_error(message, jid_from.bare) else: - text = self.core.get_error_message(message) + text = get_error_message(message) p_tab = self.core.tabs.by_name_and_class( jid_from.full, tabs.PrivateTab) if p_tab: @@ -313,7 +313,7 @@ class HandlerCore: self.core.information(text, 'Error') return tab = self.core.get_conversation_by_jid(message['from'], create=False) - error_msg = self.core.get_error_message(message, deprecated=True) + error_msg = get_error_message(message, deprecated=True) if not tab: self.core.information(error_msg, 'Error') return @@ -1749,7 +1749,7 @@ class HandlerCore: def adhoc_error(self, iq, adhoc_session): self.core.xmpp.plugin['xep_0050'].terminate_command(adhoc_session) - error_message = self.core.get_error_message(iq) + error_message = get_error_message(iq) self.core.information( "An error occurred while executing the command: %s" % (error_message), 'Error') diff --git a/poezio/core/structs.py b/poezio/core/structs.py index a75f1e94..ae4ebe7d 100644 --- a/poezio/core/structs.py +++ b/poezio/core/structs.py @@ -5,43 +5,10 @@ from dataclasses import dataclass from typing import Any, Callable, List, Dict __all__ = [ - 'ERROR_AND_STATUS_CODES', 'DEPRECATED_ERRORS', 'POSSIBLE_SHOW', 'Status', + 'POSSIBLE_SHOW', 'Status', 'Command', 'Completion' ] -# http://xmpp.org/extensions/xep-0045.html#errorstatus -ERROR_AND_STATUS_CODES = { - '401': 'A password is required', - '403': 'Permission denied', - '404': 'The room doesn’t exist', - '405': 'Your are not allowed to create a new room', - '406': 'A reserved nick must be used', - '407': 'You are not in the member list', - '409': 'This nickname is already in use or has been reserved', - '503': 'The maximum number of users has been reached', -} - -# http://xmpp.org/extensions/xep-0086.html -DEPRECATED_ERRORS = { - '302': 'Redirect', - '400': 'Bad request', - '401': 'Not authorized', - '402': 'Payment required', - '403': 'Forbidden', - '404': 'Not found', - '405': 'Not allowed', - '406': 'Not acceptable', - '407': 'Registration required', - '408': 'Request timeout', - '409': 'Conflict', - '500': 'Internal server error', - '501': 'Feature not implemented', - '502': 'Remote server error', - '503': 'Service unavailable', - '504': 'Remote server timeout', - '510': 'Disconnected', -} - POSSIBLE_SHOW = { 'available': None, 'chat': 'chat', -- cgit v1.2.3