2019-12-19 22:46:16 +11:00
|
|
|
# SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
|
2019-12-18 23:46:36 +11:00
|
|
|
"""Custom exception definitions."""
|
|
|
|
|
2019-11-12 23:38:43 +11:00
|
|
|
from dataclasses import dataclass, field
|
|
|
|
|
|
|
|
import nio
|
|
|
|
|
|
|
|
|
2019-12-12 03:42:59 +11:00
|
|
|
# Matrix Errors
|
|
|
|
|
2019-11-12 23:38:43 +11:00
|
|
|
@dataclass
|
|
|
|
class MatrixError(Exception):
|
2019-12-18 23:46:36 +11:00
|
|
|
"""An error returned by a Matrix server."""
|
|
|
|
|
2019-11-12 23:38:43 +11:00
|
|
|
http_code: int = 400
|
|
|
|
m_code: str = "M_UNKNOWN"
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_nio(cls, response: nio.ErrorResponse) -> "MatrixError":
|
2019-12-18 23:46:36 +11:00
|
|
|
"""Return a `MatrixError` subclass from a nio `ErrorResponse`."""
|
|
|
|
|
2019-11-12 23:38:43 +11:00
|
|
|
# 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()
|
|
|
|
|
|
|
|
for subcls in cls.__subclasses__():
|
|
|
|
if subcls.http_code == response.transport_response.status:
|
|
|
|
return subcls()
|
|
|
|
|
|
|
|
return cls(response.transport_response.status, response.status_code)
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class MatrixForbidden(MatrixError):
|
|
|
|
http_code: int = 403
|
|
|
|
m_code: str = "M_FORBIDDEN"
|
|
|
|
|
|
|
|
|
2019-12-12 03:42:59 +11:00
|
|
|
@dataclass
|
|
|
|
class MatrixBadJson(MatrixError):
|
|
|
|
http_code: int = 403
|
|
|
|
m_code: str = "M_BAD_JSON"
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class MatrixNotJson(MatrixError):
|
|
|
|
http_code: int = 403
|
|
|
|
m_code: str = "M_NOT_JSON"
|
|
|
|
|
|
|
|
|
2019-11-12 23:38:43 +11:00
|
|
|
@dataclass
|
|
|
|
class MatrixUserDeactivated(MatrixError):
|
|
|
|
http_code: int = 403
|
|
|
|
m_code: str = "M_USER_DEACTIVATED"
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class MatrixNotFound(MatrixError):
|
|
|
|
http_code: int = 404
|
|
|
|
m_code: str = "M_NOT_FOUND"
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class MatrixTooLarge(MatrixError):
|
|
|
|
http_code: int = 413
|
|
|
|
m_code: str = "M_TOO_LARGE"
|
|
|
|
|
|
|
|
|
2019-12-12 03:42:59 +11:00
|
|
|
# Client errors
|
|
|
|
|
2019-12-05 00:59:14 +11:00
|
|
|
@dataclass
|
|
|
|
class InvalidUserId(Exception):
|
|
|
|
user_id: str = field()
|
|
|
|
|
|
|
|
|
2019-11-12 23:38:43 +11:00
|
|
|
@dataclass
|
|
|
|
class InvalidUserInContext(Exception):
|
|
|
|
user_id: str = field()
|
|
|
|
|
|
|
|
|
2020-03-09 22:59:23 +11:00
|
|
|
@dataclass
|
|
|
|
class UserFromOtherServerDisallowed(Exception):
|
|
|
|
user_id: str = field()
|
|
|
|
|
|
|
|
|
2019-11-12 23:38:43 +11:00
|
|
|
@dataclass
|
|
|
|
class UneededThumbnail(Exception):
|
|
|
|
pass
|
|
|
|
|
2019-11-13 00:48:11 +11:00
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class BadMimeType(Exception):
|
|
|
|
wanted: str = field()
|
|
|
|
got: str = field()
|