1
Fork 0
mirror of https://github.com/RGBCube/GitHubWrapper synced 2025-05-14 13:15:00 +00:00
GitHubWrapper/github/errors.py

52 lines
1.5 KiB
Python

from __future__ import annotations
__all__ = ("GitHubError", "BaseHTTPError", "HTTPError", "RatelimitReached", "error_from_request")
from datetime import datetime, timezone
from typing import TYPE_CHECKING
from .utils import human_readable_time_until
if TYPE_CHECKING:
from aiohttp import ClientResponse
class GitHubError(Exception):
"""The base class for all errors raised in this library."""
class BaseHTTPError(GitHubError):
"""The base class for all HTTP related errors in this library."""
class HTTPError(BaseHTTPError):
"""Raised when an HTTP request doesn't respond with a successfull code."""
def __init__(self, response: ClientResponse, /) -> None:
self.method = response.method
self.code = response.status
self.url = response.url
self._response = response
def __str__(self) -> str:
return (
f"An HTTP error with the code {self.code} has occured while trying to do a"
f" {self.method} request to the URL {self.url}"
)
class RatelimitReached(GitHubError):
"""Raised when a ratelimit is reached."""
def __init__(self, reset_time: datetime, /) -> None:
self.reset_time = reset_time
def __str__(self) -> str:
return (
"The ratelimit has been reached. You can try again in"
f" {human_readable_time_until(datetime.now(timezone.utc) - self.reset_time)}"
)
def error_from_request(request: ClientResponse, /) -> BaseHTTPError:
# TODO: Make specific errrors
return HTTPError(request)