An asynchronous GitHub API library designed as a sans-I/O library for GitHub API access
—
Comprehensive exception hierarchy for GitHub API error responses, providing specific exception types for different HTTP status codes and error conditions. This enables precise error handling and appropriate responses to various GitHub API failure scenarios.
Core exception classes that form the foundation of the gidgethub exception hierarchy.
class GitHubException(Exception):
"""Base exception for this library."""
class ValidationFailure(GitHubException):
"""An exception representing failed validation of a webhook event."""Exception classes for HTTP response status codes with detailed error information.
class HTTPException(GitHubException):
"""A general exception to represent HTTP responses."""
def __init__(self, status_code: http.HTTPStatus, *args: Any) -> None: ...
# Attributes
status_code: http.HTTPStatus
class RedirectionException(HTTPException):
"""Exception for 3XX HTTP responses."""
class GitHubBroken(HTTPException):
"""Exception for 5XX HTTP responses."""Exception classes for 4XX HTTP status codes representing client-side errors.
class BadRequest(HTTPException):
"""The request is invalid. Used for 4XX HTTP errors."""
class BadRequestUnknownError(BadRequest):
"""A bad request whose response body is not JSON."""
def __init__(self, response: str) -> None: ...
# Attributes
response: str
class RateLimitExceeded(BadRequest):
"""Request rejected due to the rate limit being exceeded."""
def __init__(self, rate_limit: Any, *args: Any) -> None: ...
# Attributes
rate_limit: Any # Actually gidgethub.sansio.RateLimit
class InvalidField(BadRequest):
"""A field in the request is invalid. Represented by a 422 HTTP Response."""
def __init__(self, errors: Any, *args: Any) -> None: ...
# Attributes
errors: Any
class ValidationError(BadRequest):
"""A request was unable to be completed. Represented by a 422 HTTP response."""
def __init__(self, errors: Any, *args: Any) -> None: ...
# Attributes
errors: AnyException classes specific to GitHub's GraphQL v4 API.
class GraphQLException(GitHubException):
"""Base exception for the GraphQL v4 API."""
def __init__(self, message: str, response: Any) -> None: ...
# Attributes
response: Any
class BadGraphQLRequest(GraphQLException):
"""A 4XX HTTP response."""
def __init__(self, status_code: http.HTTPStatus, response: Any) -> None: ...
# Attributes
status_code: http.HTTPStatus
class GraphQLAuthorizationFailure(BadGraphQLRequest):
"""401 HTTP response to a bad oauth token."""
def __init__(self, response: Any) -> None: ...
class QueryError(GraphQLException):
"""An error occurred while attempting to handle a GraphQL v4 query."""
def __init__(self, response: Any) -> None: ...
class GraphQLResponseTypeError(GraphQLException):
"""The GraphQL response has an unexpected content type."""
def __init__(self, content_type: Optional[str], response: Any) -> None: ...import asyncio
from gidgethub.aiohttp import GitHubAPI
from gidgethub import RateLimitExceeded
async def api_call_with_retry(gh: GitHubAPI, url: str):
try:
return await gh.getitem(url)
except RateLimitExceeded as exc:
# Wait until rate limit resets
if exc.rate_limit:
sleep_time = (exc.rate_limit.reset_datetime -
datetime.datetime.now(datetime.timezone.utc)).total_seconds()
if sleep_time > 0:
await asyncio.sleep(sleep_time)
return await gh.getitem(url)
raisefrom gidgethub import InvalidField, ValidationError
async def create_issue(gh: GitHubAPI, repo: str, title: str, body: str):
try:
return await gh.post(f"/repos/{repo}/issues",
data={"title": title, "body": body})
except InvalidField as exc:
print(f"Invalid fields: {exc.errors}")
raise
except ValidationError as exc:
print(f"Validation failed: {exc.errors}")
raisefrom gidgethub import QueryError, GraphQLAuthorizationFailure
async def graphql_query(gh: GitHubAPI, query: str, **variables):
try:
return await gh.graphql(query, **variables)
except GraphQLAuthorizationFailure:
print("Invalid or expired OAuth token")
raise
except QueryError as exc:
print(f"GraphQL query error: {exc}")
print(f"Response: {exc.response}")
raiseimport http
from typing import Any, Optional
# All exception classes inherit from these base types
GitHubException = Exception
HTTPException = GitHubException
BadRequest = HTTPException
GraphQLException = GitHubExceptionInstall with Tessl CLI
npx tessl i tessl/pypi-gidgethub