2020-09-24 09:57:54 +10:00
|
|
|
# Copyright Mirage authors & contributors <https://github.com/mirukana/mirage>
|
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
|
2020-05-30 06:45:08 +10:00
|
|
|
from typing import Optional
|
2019-11-12 23:38:43 +11:00
|
|
|
|
|
|
|
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."""
|
|
|
|
|
2020-05-30 06:45:08 +10:00
|
|
|
http_code: int = 400
|
|
|
|
m_code: Optional[str] = None
|
2019-11-12 23:38:43 +11:00
|
|
|
|
|
|
|
@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`."""
|
|
|
|
|
2020-10-19 05:28:41 +11:00
|
|
|
http_code = response.transport_response.status
|
|
|
|
m_code = response.status_code
|
|
|
|
|
2019-11-12 23:38:43 +11:00
|
|
|
for subcls in cls.__subclasses__():
|
2020-10-19 05:28:41 +11:00
|
|
|
if subcls.m_code and subcls.m_code == m_code:
|
2019-11-12 23:38:43 +11:00
|
|
|
return subcls()
|
|
|
|
|
2020-10-19 05:28:41 +11:00
|
|
|
# If error doesn't have a M_CODE, look for a generic http error class
|
2019-11-12 23:38:43 +11:00
|
|
|
for subcls in cls.__subclasses__():
|
2020-10-19 05:28:41 +11:00
|
|
|
if not subcls.m_code and subcls.http_code == http_code:
|
2019-11-12 23:38:43 +11:00
|
|
|
return subcls()
|
|
|
|
|
2020-10-19 05:28:41 +11:00
|
|
|
return cls(http_code, m_code)
|
2019-11-12 23:38:43 +11:00
|
|
|
|
|
|
|
|
2020-09-14 00:30:40 +10:00
|
|
|
@dataclass
|
|
|
|
class MatrixUnrecognized(MatrixError):
|
|
|
|
http_code: int = 400
|
|
|
|
m_code: str = "M_UNRECOGNIZED"
|
|
|
|
|
|
|
|
|
2020-09-05 01:21:36 +10:00
|
|
|
@dataclass
|
|
|
|
class MatrixInvalidAccessToken(MatrixError):
|
|
|
|
http_code: int = 401
|
|
|
|
m_code: str = "M_UNKNOWN_TOKEN"
|
|
|
|
|
|
|
|
|
2020-06-30 00:30:44 +10:00
|
|
|
@dataclass
|
|
|
|
class MatrixUnauthorized(MatrixError):
|
|
|
|
http_code: int = 401
|
|
|
|
m_code: str = "M_UNAUTHORIZED"
|
|
|
|
|
|
|
|
|
2019-11-12 23:38:43 +11:00
|
|
|
@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"
|
|
|
|
|
|
|
|
|
2021-01-21 06:50:04 +11:00
|
|
|
@dataclass
|
|
|
|
class MatrixBadGateway(MatrixError):
|
|
|
|
http_code: int = 502
|
|
|
|
m_code: Optional[str] = None
|
|
|
|
|
2020-05-30 06:45:08 +10:00
|
|
|
@dataclass
|
|
|
|
class MatrixBadGateway(MatrixError):
|
2020-10-19 05:28:41 +11:00
|
|
|
http_code: int = 502
|
|
|
|
m_code: Optional[str] = None
|
2020-05-30 06:45:08 +10:00
|
|
|
|
|
|
|
|
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()
|