Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: improve request error structure #145

Merged
merged 1 commit into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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 @@
"""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 @@
)


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
Loading