From 32679aa7f8ff426d588e2eb04b6cde5bf1db5ec0 Mon Sep 17 00:00:00 2001 From: miruka Date: Sun, 13 Sep 2020 10:30:40 -0400 Subject: [PATCH] Abandon fetching offline presence on 400 error It seems like some servers return a M_UNRECOGNIZED code instead of M_FORBIDDEN or "offline" when trying to fetch the presence for a user and it's not available. Fixes https://github.com/mirukana/mirage/issues/116 --- src/backend/errors.py | 6 ++++++ src/backend/matrix_client.py | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/backend/errors.py b/src/backend/errors.py index 0c2bfc5f..3b468fa9 100644 --- a/src/backend/errors.py +++ b/src/backend/errors.py @@ -33,6 +33,12 @@ class MatrixError(Exception): return cls(response.transport_response.status, response.status_code) +@dataclass +class MatrixUnrecognized(MatrixError): + http_code: int = 400 + m_code: str = "M_UNRECOGNIZED" + + @dataclass class MatrixInvalidAccessToken(MatrixError): http_code: int = 401 diff --git a/src/backend/matrix_client.py b/src/backend/matrix_client.py index bb210ded..7aca3d98 100644 --- a/src/backend/matrix_client.py +++ b/src/backend/matrix_client.py @@ -37,7 +37,7 @@ from . import __app_name__, __display_name__, utils from .errors import ( BadMimeType, InvalidUserId, InvalidUserInContext, MatrixBadGateway, MatrixError, MatrixForbidden, MatrixInvalidAccessToken, MatrixNotFound, - MatrixTooLarge, MatrixUnauthorized, UneededThumbnail, + MatrixTooLarge, MatrixUnauthorized, MatrixUnrecognized, UneededThumbnail, UserFromOtherServerDisallowed, ) from .html_markdown import HTML_PROCESSOR as HTML @@ -1474,7 +1474,7 @@ class MatrixClient(nio.AsyncClient): try: async with self.backend.concurrent_get_presence_limit: resp = await self.get_presence(user_id) - except MatrixForbidden: + except (MatrixForbidden, MatrixUnrecognized): return await self.nio_callbacks.onPresenceEvent(nio.PresenceEvent(