1
Fork 0
mirror of https://github.com/RGBCube/GitHubWrapper synced 2025-05-14 13:15:00 +00:00
GitHubWrapper/github/errors.py
RGBCube b23d5b78eb Rewrote the library
- Only HTTPClient, File and Object exists currently
2022-06-25 14:11:17 +03:00

48 lines
1.4 KiB
Python

from __future__ import annotations
__all__ = ("GitHubError", "BaseHTTPError", "HTTPError", "RatelimitReached")
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)}"
)