MatrixError: include server response content

This commit is contained in:
miruka 2021-07-13 23:13:34 -04:00
parent f2b981e288
commit 42b2a040ec
2 changed files with 8 additions and 6 deletions

View File

@ -17,25 +17,27 @@ class MatrixError(Exception):
http_code: int = 400
m_code: Optional[str] = None
message: Optional[str] = None
content: str = ""
@classmethod
def from_nio(cls, response: nio.ErrorResponse) -> "MatrixError":
async def from_nio(cls, response: nio.ErrorResponse) -> "MatrixError":
"""Return a `MatrixError` subclass from a nio `ErrorResponse`."""
http_code = response.transport_response.status
m_code = response.status_code
message = response.message
content = await response.transport_response.text()
for subcls in cls.__subclasses__():
if subcls.m_code and subcls.m_code == m_code:
return subcls(http_code, m_code, message)
return subcls(http_code, m_code, message, content)
# If error doesn't have a M_CODE, look for a generic http error class
for subcls in cls.__subclasses__():
if not subcls.m_code and subcls.http_code == http_code:
return subcls(http_code, m_code, message)
return subcls(http_code, m_code, message, content)
return cls(http_code, m_code, message)
return cls(http_code, m_code, message, content)
@dataclass

View File

@ -266,7 +266,7 @@ class MatrixClient(nio.AsyncClient):
if isinstance(response, nio.ErrorResponse):
try:
raise MatrixError.from_nio(response)
raise await MatrixError.from_nio(response)
except MatrixInvalidAccessToken:
if not self.invalid_disconnecting:
self.invalid_disconnecting = True
@ -1292,7 +1292,7 @@ class MatrixClient(nio.AsyncClient):
for user_id, response in zip(user_ids, responses):
if isinstance(response, nio.RoomInviteError):
errors.append((user_id, MatrixError.from_nio(response)))
errors.append((user_id, await MatrixError.from_nio(response)))
elif isinstance(response, Exception):
errors.append((user_id, response))