From e95a23ef9012b4f79f6583107e5fb8f483a1f187 Mon Sep 17 00:00:00 2001 From: miruka Date: Sat, 9 Nov 2019 10:20:16 -0400 Subject: [PATCH] Handle JoinRoom errors, error clases overhaul --- src/python/matrix_client.py | 32 +++++++++++++++++++++++++----- src/qml/Pages/AddChat/JoinRoom.qml | 22 ++++++++++++++++++-- 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/src/python/matrix_client.py b/src/python/matrix_client.py index aeba2e17..79313f54 100644 --- a/src/python/matrix_client.py +++ b/src/python/matrix_client.py @@ -35,8 +35,27 @@ CryptDict = Dict[str, Any] @dataclass -class RequestFailed(Exception): - m_code: Optional[str] = None +class MatrixError(Exception): + m_code: str = "M_UNKNOWN" + + @classmethod + def from_nio(cls, response: nio.ErrorResponse) -> "MatrixError": + for subcls in cls.__subclasses__(): + if subcls.m_code == response.status_code: + return subcls() + + return cls(response.status_code) + + +@dataclass +class MatrixNotFound(MatrixError): + m_code: str = "M_NOT_FOUND" + + +@dataclass +class MatrixForbidden(MatrixError): + m_code: str = "M_FORBIDDEN" + @dataclass class UploadError(Exception): @@ -448,7 +467,7 @@ class MatrixClient(nio.AsyncClient): ) if isinstance(response, nio.RoomCreateError): - raise RequestFailed(response.status_code) + raise MatrixError.from_nio(response) return response.room_id @@ -457,16 +476,19 @@ class MatrixClient(nio.AsyncClient): if re.match(r"^https?://", string): for part in urlparse(string).fragment.split("/"): - if re.match(r"[#!].+:.+", part): + if re.match(r"^[#!].+:.+", part): string = part break else: raise ValueError(f"No alias or room id found in url {string}") + if not re.match(r"^[#!].+:.+", string): + raise ValueError("Not an alias or room id") + response = await super().join(string) if isinstance(response, nio.JoinError): - raise RequestFailed(response.status_code) + raise MatrixError.from_nio(response) return response.room_id diff --git a/src/qml/Pages/AddChat/JoinRoom.qml b/src/qml/Pages/AddChat/JoinRoom.qml index ca3e936f..9fbe8f44 100644 --- a/src/qml/Pages/AddChat/JoinRoom.qml +++ b/src/qml/Pages/AddChat/JoinRoom.qml @@ -21,13 +21,31 @@ HBox { let args = [roomField.text] py.callClientCoro(userId, "room_join", args, roomId => { - button.loading = false + button.loading = false + errorMessage.text = "" pageLoader.showRoom(userId, roomId) + + }, (type, args) => { + button.loading = false + + let txt = qsTr("Unknown error - %1, %2").arg(type).arg(args) + + if (type === "ValueError") + txt = qsTr("Unrecognized alias, room ID or URL") + + if (type === "MatrixNotFound") + txt = qsTr("Room not found") + + if (type === "MatrixForbidden") + txt = qsTr("You do not have permission to join this room") + + errorMessage.text = txt }) }, cancel: button => { - roomField.text = "" + roomField.text = "" + errorMessage.text = "" pageLoader.showPrevious() } })