mirror of
https://github.com/RGBCube/GitHubWrapper
synced 2025-05-14 05:04:59 +00:00
53 lines
1.5 KiB
Python
53 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 successful 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 occurred 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 errors
|
|
return HTTPError(request)
|