Skip to content

Commit

Permalink
✨ improve request error structure
Browse files Browse the repository at this point in the history
  • Loading branch information
yanyongyu authored Oct 5, 2024
1 parent 3865ae3 commit d7ca09e
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

Check warning on line 286 in githubkit/core.py

View check run for this annotation

Codecov / codecov/patch

githubkit/core.py#L286

Added line #L286 was not covered by tests
except Exception as e:
raise RequestError(repr(e)) from e
raise RequestError(e) from e

Check warning on line 288 in githubkit/core.py

View check run for this annotation

Codecov / codecov/patch

githubkit/core.py#L288

Added line #L288 was not covered by tests

# 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

Check warning on line 318 in githubkit/core.py

View check run for this annotation

Codecov / codecov/patch

githubkit/core.py#L318

Added line #L318 was not covered by tests
except Exception as e:
raise RequestError(repr(e)) from e
raise RequestError(e) from e

Check warning on line 320 in githubkit/core.py

View check run for this annotation

Codecov / codecov/patch

githubkit/core.py#L320

Added line #L320 was not covered by tests

# 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

Check warning on line 29 in githubkit/exception.py

View check run for this annotation

Codecov / codecov/patch

githubkit/exception.py#L29

Added line #L29 was not covered by tests

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

Check warning on line 40 in githubkit/exception.py

View check run for this annotation

Codecov / codecov/patch

githubkit/exception.py#L40

Added line #L40 was not covered by tests

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__(

Check warning on line 53 in githubkit/exception.py

View check run for this annotation

Codecov / codecov/patch

githubkit/exception.py#L53

Added line #L53 was not covered by tests
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 d7ca09e

Please sign in to comment.