Properly handle login errors with new exceptions
This commit is contained in:
@@ -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())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user