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

GHASToolkit errors #243

Merged
merged 3 commits into from
Jul 29, 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
2 changes: 2 additions & 0 deletions src/ghastoolkit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"""


from ghastoolkit.errors import GHASToolkitError, GHASToolkitAuthenticationError

# Octokit
from ghastoolkit.octokit.github import GitHub
from ghastoolkit.octokit.repository import Repository
Expand Down
41 changes: 41 additions & 0 deletions src/ghastoolkit/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# GHASToolkit Errors


from typing import List, Optional


class GHASToolkitError(Exception):
"""Base class for GHASToolkit errors."""

def __init__(
self,
message: Optional[str] = None,
docs: Optional[str] = None,
permissions: Optional[List[str]] = [],
) -> None:
self.message = message
self.docs = docs
self.permissions = permissions

super().__init__(message)

def __str__(self) -> str:
msg = ""

if hasattr(self, "message"):
msg = self.message
else:
msg = "An error occurred"

if permissions := self.permissions:
msg += "\n\nPermissions Required:"
for perm in permissions:
msg += f"\n- {perm}"
if docs := self.docs:
msg += f"\n\nFor more information, see: {docs}"

return msg


class GHASToolkitAuthenticationError(GHASToolkitError):
"""Raised when an authentication error occurs."""
13 changes: 9 additions & 4 deletions src/ghastoolkit/octokit/octokit.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from requests import Session
from ratelimit import limits, sleep_and_retry

from ghastoolkit.errors import GHASToolkitAuthenticationError, GHASToolkitError
from ghastoolkit.octokit.github import GitHub, Repository
from ghastoolkit.octokit.graphql import QUERIES

Expand All @@ -19,7 +20,9 @@

__OCTOKIT_PATH__ = os.path.dirname(os.path.realpath(__file__))

__OCTOKIT_ERRORS__ = {401: "Authentication Issue"}
__OCTOKIT_ERRORS__ = {
401: GHASToolkitAuthenticationError("Authentication / Permission Issue")
}


# logger
Expand Down Expand Up @@ -195,7 +198,9 @@ def get(
logger.debug(f"Fetching content from URL :: {url}")

if authenticated and not self.session.headers.get("Authorization"):
raise Exception(f"GitHub Token required for this request")
raise GHASToolkitAuthenticationError(
"GitHub Token required for this request"
)

result = []
params = {}
Expand All @@ -221,7 +226,7 @@ def get(

known_error = __OCTOKIT_ERRORS__.get(response.status_code)
if known_error:
raise Exception(known_error)
raise known_error

# Handle errors in the response
if isinstance(response_json, dict) and response_json.get("message"):
Expand All @@ -238,7 +243,7 @@ def get(
logger.error(f"Error message from server :: {message}")
logger.error(f"Documentation Link :: {docs}")

raise Exception(f"REST Request failed :: {message}")
raise GHASToolkitError(f"REST Request failed :: {message}", docs=docs)

if isinstance(response_json, dict):
return response_json
Expand Down