From 877290fc00748d5dd38089292d1ee9f4c1c0640f Mon Sep 17 00:00:00 2001 From: miruka Date: Fri, 29 May 2020 16:45:08 -0400 Subject: [PATCH] Handle 502 errors when inviting bad user to room --- src/backend/errors.py | 14 ++++++++++---- src/backend/matrix_client.py | 6 +++--- src/gui/Popups/InviteToRoomPopup.qml | 4 ++++ 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/backend/errors.py b/src/backend/errors.py index 23f987df..5d3a3923 100644 --- a/src/backend/errors.py +++ b/src/backend/errors.py @@ -3,18 +3,18 @@ """Custom exception definitions.""" from dataclasses import dataclass, field +from typing import Optional import nio - # Matrix Errors @dataclass class MatrixError(Exception): """An error returned by a Matrix server.""" - http_code: int = 400 - m_code: str = "M_UNKNOWN" + http_code: int = 400 + m_code: Optional[str] = None @classmethod def from_nio(cls, response: nio.ErrorResponse) -> "MatrixError": @@ -23,7 +23,7 @@ class MatrixError(Exception): # Check for the M_CODE first: some errors for an API share the same # http code, but have different M_CODEs (e.g. POST /login 403). for subcls in cls.__subclasses__(): - if subcls.m_code == response.status_code: + if subcls.m_code and subcls.m_code == response.status_code: return subcls() for subcls in cls.__subclasses__(): @@ -69,6 +69,12 @@ class MatrixTooLarge(MatrixError): m_code: str = "M_TOO_LARGE" +@dataclass +class MatrixBadGateway(MatrixError): + http_code: int = 502 + m_code: str = "" + + # Client errors @dataclass diff --git a/src/backend/matrix_client.py b/src/backend/matrix_client.py index fb267581..a57d2e68 100644 --- a/src/backend/matrix_client.py +++ b/src/backend/matrix_client.py @@ -32,8 +32,8 @@ from nio.crypto import async_generator_from_data from . import __app_name__, __display_name__, utils from .errors import ( - BadMimeType, InvalidUserId, InvalidUserInContext, MatrixError, - MatrixNotFound, MatrixTooLarge, UneededThumbnail, + BadMimeType, InvalidUserId, InvalidUserInContext, MatrixBadGateway, + MatrixError, MatrixNotFound, MatrixTooLarge, UneededThumbnail, UserFromOtherServerDisallowed, ) from .html_markdown import HTML_PROCESSOR as HTML @@ -964,7 +964,7 @@ class MatrixClient(nio.AsyncClient): try: await self.get_profile(user_id) - except MatrixNotFound as err: + except (MatrixNotFound, MatrixBadGateway) as err: return err return await self.room_invite(room_id, user_id) diff --git a/src/gui/Popups/InviteToRoomPopup.qml b/src/gui/Popups/InviteToRoomPopup.qml index a7105db2..ff3b4474 100644 --- a/src/gui/Popups/InviteToRoomPopup.qml +++ b/src/gui/Popups/InviteToRoomPopup.qml @@ -108,6 +108,10 @@ BoxPopup { qsTr("%1 not found, please verify the entered user ID") .arg(user) : + type === "MatrixBadGateway" ? + qsTr("Server error while trying to find %1, please " + + "verify the entered user ID").arg(user) : + type === "MatrixUnsupportedRoomVersion" ? qsTr("%1's server does not support this room's version") .arg(user) :