Source code for anmoku.errors

from __future__ import annotations
from typing import TYPE_CHECKING, Optional, TypedDict

if TYPE_CHECKING:
    from typing import Type
    from typing_extensions import NotRequired

    from .resources import JikanResource

from .logger import anmoku_logger
from devgoldyutils import Colours

__all__ = (
    "ResourceNotSupportedError",
    "ResourceRequiresError",
    "HTTPError",
    "NotFoundError",
    "RatelimitError",
    "ServerError",
)

class AnmokuException(Exception):
    message: str

    def __init__(self, message: str):
        self.message = message

        formatted_message = Colours.RED.apply_to_string(message)

        anmoku_logger.critical(formatted_message)
        super().__init__(formatted_message)

[docs] class ResourceNotSupportedError(AnmokuException): def __init__(self, resource: Type[JikanResource], not_supported: str): super().__init__( f"The '{resource.__name__}' resource does not support {not_supported}!" )
[docs] class ResourceRequiresError(AnmokuException): def __init__(self, resource: Type[JikanResource], parameter_required: str): super().__init__( f"The '{resource.__name__}' resource requires the keyword parameter '{parameter_required}' to be passed! " \ f"E.g. client.get(resource, id = 1, {parameter_required} = 1)" )
[docs] class HTTPError(AnmokuException): __slots__ = ( "status", "type", "message", "report_url", ) def __init__(self, resp: ErrorResponseDict): self.type: str = resp["type"] self.status: int = resp["status"] self.message: str = resp["message"] self.report_url: Optional[str] = resp.get("report_url") # TODO: error trace? message = f"{self.type} (status code {self.status}): {self.message}" if self.report_url is not None: message += f"\nYou can report this issue to the Jikan API via GitHub: {self.report_url}" super().__init__(message)
[docs] class NotFoundError(HTTPError): __slots__ = ()
[docs] class RatelimitError(HTTPError): __slots__ = ()
[docs] class ServerError(HTTPError): __slots__ = ()
class ErrorResponseDict(TypedDict): status: int type: str message: str error: str report_url: NotRequired[str]