Document errors module

This commit is contained in:
miruka 2019-12-18 08:46:36 -04:00
parent 61cd3b2f55
commit ab46ac15e7

View File

@ -1,3 +1,5 @@
"""Custom exception definitions."""
from dataclasses import dataclass, field from dataclasses import dataclass, field
import nio import nio
@ -7,11 +9,15 @@ import nio
@dataclass @dataclass
class MatrixError(Exception): class MatrixError(Exception):
"""An error returned by a Matrix server."""
http_code: int = 400 http_code: int = 400
m_code: str = "M_UNKNOWN" m_code: str = "M_UNKNOWN"
@classmethod @classmethod
def from_nio(cls, response: nio.ErrorResponse) -> "MatrixError": def from_nio(cls, response: nio.ErrorResponse) -> "MatrixError":
"""Return a `MatrixError` subclass from a nio `ErrorResponse`."""
# Check for the M_CODE first: some errors for an API share the same # 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). # http code, but have different M_CODEs (e.g. POST /login 403).
for subcls in cls.__subclasses__(): for subcls in cls.__subclasses__():