Skip to content

Commit b7960a9

Browse files
committed
feat: Update known errors and tests
1 parent dfe78b7 commit b7960a9

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

src/ghastoolkit/errors.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ def __init__(
1212
message: Optional[str] = None,
1313
docs: Optional[str] = None,
1414
permissions: Optional[List[str]] = [],
15+
status: Optional[int] = None,
1516
) -> None:
1617
self.message = message
1718
self.docs = docs
1819
self.permissions = permissions
20+
self.status = status
1921

2022
super().__init__(message)
2123

@@ -27,6 +29,9 @@ def __str__(self) -> str:
2729
else:
2830
msg = "An error occurred"
2931

32+
if status := self.status:
33+
msg += f" (status code: {status})"
34+
3035
if permissions := self.permissions:
3136
msg += "\n\nPermissions Required:"
3237
for perm in permissions:

src/ghastoolkit/octokit/octokit.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@
2121
__OCTOKIT_PATH__ = os.path.dirname(os.path.realpath(__file__))
2222

2323
__OCTOKIT_ERRORS__ = {
24-
401: GHASToolkitAuthenticationError("Authentication / Permission Issue"),
25-
403: GHASToolkitAuthenticationError("Authentication / Permission Issue"),
26-
404: GHASToolkitError("Not Found"),
27-
429: GHASToolkitError("Rate limit hit"),
28-
500: GHASToolkitError("GitHub Server Error"),
24+
401: GHASToolkitAuthenticationError(
25+
"Authentication / Permission Issue", status=401
26+
),
27+
403: GHASToolkitAuthenticationError(
28+
"Authentication / Permission Issue", status=403
29+
),
30+
404: GHASToolkitError("Not Found", status=404),
31+
429: GHASToolkitError("Rate limit hit", status=429),
32+
500: GHASToolkitError("GitHub Server Error", status=500),
2933
}
3034

3135

@@ -228,6 +232,9 @@ def get(
228232
if display_errors:
229233
logger.error(f"Error code from server :: {response.status_code}")
230234

235+
if error_handler:
236+
return error_handler(response.status_code, response_json)
237+
231238
known_error = __OCTOKIT_ERRORS__.get(response.status_code)
232239
if known_error:
233240
raise known_error

tests/test_restrequest.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Test RestRequest class."""
2+
23
import unittest
4+
from ghastoolkit.errors import GHASToolkitError
35
import responses
46

57
from ghastoolkit.octokit.octokit import RestRequest
@@ -35,10 +37,10 @@ def test_errors(self):
3537
status=404,
3638
)
3739

38-
with self.assertRaises(Exception):
40+
with self.assertRaises(GHASToolkitError):
3941
self.rest.get("/repos/{owner}/{repo}/secret-scanning/alerts")
4042

41-
with self.assertRaises(Exception):
43+
with self.assertRaises(GHASToolkitError):
4244
self.rest.get("/repos/{owner}/{repo}/secret-scanning/alerts/1")
4345

4446
@responses.activate

0 commit comments

Comments
 (0)