Properly handle login errors with new exceptions

This commit is contained in:
miruka
2019-11-11 06:39:11 -04:00
parent de7053f196
commit 7f48c1b35d
4 changed files with 48 additions and 15 deletions

View File

@@ -41,6 +41,8 @@ class MatrixError(Exception):
@classmethod
def from_nio(cls, response: nio.ErrorResponse) -> "MatrixError":
# 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:
return subcls()
@@ -58,6 +60,12 @@ class MatrixForbidden(MatrixError):
m_code: str = "M_FORBIDDEN"
@dataclass
class MatrixUserDeactivated(MatrixError):
http_code: int = 403
m_code: str = "M_USER_DEACTIVATED"
@dataclass
class MatrixNotFound(MatrixError):
http_code: int = 404
@@ -159,7 +167,7 @@ class MatrixClient(nio.AsyncClient):
)
if isinstance(response, nio.LoginError):
raise RuntimeError(response)
raise MatrixError.from_nio(response)
asyncio.ensure_future(self.start())