Skip to content

Commit

Permalink
✨ Feature: improve request error structure (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyongyu authored Oct 5, 2024
1 parent 3865ae3 commit 270b57c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
10 changes: 5 additions & 5 deletions docs/usage/error-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ GitHubException
├── AuthCredentialError
├── AuthExpiredError
├── RequestError
├── RequestTimeout
── RequestFailed
│ └── RateLimitExceeded
│ ├── PrimaryRateLimitExceeded
│ └── SecondaryRateLimitExceeded
├── RequestTimeout
│ └── RequestFailed
└── RateLimitExceeded
├── PrimaryRateLimitExceeded
└── SecondaryRateLimitExceeded
├── GraphQLError
│ ├── GraphQLFailed
│ └── GraphQLPaginationError
Expand Down
8 changes: 4 additions & 4 deletions githubkit/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,9 @@ def _request(
cookies=cookies,
)
except httpx.TimeoutException as e:
raise RequestTimeout(e.request) from e
raise RequestTimeout(e) from e
except Exception as e:
raise RequestError(repr(e)) from e
raise RequestError(e) from e

# async request
async def _arequest(
Expand Down Expand Up @@ -315,9 +315,9 @@ async def _arequest(
cookies=cookies,
)
except httpx.TimeoutException as e:
raise RequestTimeout(e.request) from e
raise RequestTimeout(e) from e
except Exception as e:
raise RequestError(repr(e)) from e
raise RequestError(e) from e

# check and parse response
@overload
Expand Down
29 changes: 23 additions & 6 deletions githubkit/exception.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import timedelta
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Generic, TypeVar

import httpx

Expand All @@ -8,6 +8,9 @@
from .graphql import GraphQLResponse


E = TypeVar("E", bound=Exception)


class GitHubException(Exception): ...


Expand All @@ -19,15 +22,22 @@ class AuthExpiredError(GitHubException):
"""Auth Expired Error"""


class RequestError(GitHubException):
class RequestError(GitHubException, Generic[E]):
"""Simple API request failed with unknown error"""

def __init__(self, exc: E) -> None:
self.exc = exc

def __repr__(self) -> str:
return f"{self.__class__.__name__}(origin_exc={self.exc!r})"


class RequestTimeout(GitHubException):
class RequestTimeout(RequestError[httpx.TimeoutException]):
"""Simple API request timeout"""

def __init__(self, request: httpx.Request):
self.request = request
@property
def request(self) -> httpx.Request:
return self.exc.request

def __repr__(self) -> str:
return (
Expand All @@ -36,10 +46,17 @@ def __repr__(self) -> str:
)


class RequestFailed(GitHubException):
class RequestFailed(RequestError[httpx.HTTPStatusError]):
"""Simple API request failed with error status code"""

def __init__(self, response: "Response"):
super().__init__(
httpx.HTTPStatusError(
"Request failed",
request=response.raw_request,
response=response.raw_response,
)
)
self.request = response.raw_request
self.response = response

Expand Down

0 comments on commit 270b57c

Please sign in to comment.