From 6558bdc87f714243f59f22fedae2a5e15e2d8c93 Mon Sep 17 00:00:00 2001 From: miruka Date: Tue, 12 Nov 2019 08:38:43 -0400 Subject: [PATCH] Put custom exceptions in their own file --- src/python/errors.py | 67 ++++++++++++++++++++++++++++++++++++ src/python/matrix_client.py | 68 +++---------------------------------- 2 files changed, 71 insertions(+), 64 deletions(-) create mode 100644 src/python/errors.py diff --git a/src/python/errors.py b/src/python/errors.py new file mode 100644 index 00000000..5821726d --- /dev/null +++ b/src/python/errors.py @@ -0,0 +1,67 @@ +from dataclasses import dataclass, field +from typing import Optional + +import nio + + +@dataclass +class MatrixError(Exception): + http_code: int = 400 + m_code: str = "M_UNKNOWN" + + @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() + + 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" + + +@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" + + +@dataclass +class UserNotFound(Exception): + user_id: str = field() + + +@dataclass +class InvalidUserInContext(Exception): + user_id: str = field() + + +@dataclass +class UneededThumbnail(Exception): + pass + +@dataclass +class UnthumbnailableError(Exception): + exception: Optional[Exception] = None diff --git a/src/python/matrix_client.py b/src/python/matrix_client.py index 1277be37..78ed9afd 100644 --- a/src/python/matrix_client.py +++ b/src/python/matrix_client.py @@ -7,7 +7,6 @@ import platform import re import traceback from contextlib import suppress -from dataclasses import dataclass, field from datetime import datetime from functools import partial from pathlib import Path @@ -24,6 +23,10 @@ from pymediainfo import MediaInfo import nio from . import __about__, utils +from .errors import ( + InvalidUserInContext, MatrixError, UneededThumbnail, UnthumbnailableError, + UserNotFound, +) from .html_filter import HTML_FILTER from .models.items import ( Account, Event, Member, Room, TypeSpecifier, Upload, UploadStatus, @@ -34,69 +37,6 @@ from .pyotherside_events import AlertRequested CryptDict = Dict[str, Any] -@dataclass -class MatrixError(Exception): - http_code: int = 400 - m_code: str = "M_UNKNOWN" - - @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() - - 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" - - -@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" - - -@dataclass -class UserNotFound(Exception): - user_id: str = field() - - -@dataclass -class InvalidUserInContext(Exception): - user_id: str = field() - - -@dataclass -class UneededThumbnail(Exception): - pass - -@dataclass -class UnthumbnailableError(Exception): - exception: Optional[Exception] = None - - class MatrixClient(nio.AsyncClient): def __init__(self, backend,