Handle 502 errors when inviting bad user to room

This commit is contained in:
miruka 2020-05-29 16:45:08 -04:00
parent 01d3b6b489
commit 877290fc00
3 changed files with 17 additions and 7 deletions

View File

@ -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

View File

@ -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)

View File

@ -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) :