Skip to content

Commit ddd437a

Browse files
Export headers in GithubException (#1887)
Since the headers that led to an exception are also useful, firstly pass them into the constructor, and then export them in a property. Test one specific use case to make sure of coverage. Fixes #1814
1 parent e0acd8f commit ddd437a

5 files changed

Lines changed: 20 additions & 7 deletions

File tree

github/GithubException.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ class GithubException(Exception):
3838
Some other types of exceptions might be raised by underlying libraries, for example for network-related issues.
3939
"""
4040

41-
def __init__(self, status, data):
41+
def __init__(self, status, data, headers):
4242
super().__init__()
4343
self.__status = status
4444
self.__data = data
45-
self.args = [status, data]
45+
self.__headers = headers
46+
self.args = [status, data, headers]
4647

4748
@property
4849
def status(self):
@@ -58,6 +59,13 @@ def data(self):
5859
"""
5960
return self.__data
6061

62+
@property
63+
def headers(self):
64+
"""
65+
The headers returned by the Github API
66+
"""
67+
return self.__headers
68+
6169
def __str__(self):
6270
return "{status} {data}".format(status=self.status, data=json.dumps(self.data))
6371

github/GithubException.pyi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
from typing import Any, Dict, List, Optional, Tuple, Type, Union
22

33
class GithubException(Exception):
4-
def __init__(self, status: Union[int, str], data: Any,) -> None: ...
4+
def __init__(self, status: Union[int, str], data: Any, headers: Optional[Dict[str, str]]) -> None: ...
55
def __str__(self) -> str: ...
66
@property
77
def data(self) -> Dict[str, Union[str, List[str], List[Dict[str, str]]]]: ...
88
@property
99
def status(self) -> int: ...
1010

11+
@property
12+
def headers(self) -> Union[None, Dict[str, str]]: ...
13+
1114
class BadAttributeException(GithubException):
1215
def __init__(
1316
self,

github/GithubObject.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ def _completeIfNeeded(self):
300300
def __complete(self):
301301
if self._url.value is None:
302302
raise GithubException.IncompletableObject(
303-
400, "Returned object contains no URL"
303+
400, "Returned object contains no URL", None
304304
)
305305
headers, data = self._requester.requestJsonAndCheck("GET", self._url.value)
306306
self._storeAndUseAttributes(headers, data)

github/Requester.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ def __createException(self, status, headers, output):
435435
cls = GithubException.UnknownObjectException
436436
else:
437437
cls = GithubException.GithubException
438-
return cls(status, output)
438+
return cls(status, output, headers)
439439

440440
def __structuredFromJson(self, data):
441441
if len(data) == 0:

tests/Exceptions.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def testBadAuthentication(self):
9090
self.assertEqual(str(raisedexp.exception), '401 {"message": "Bad credentials"}')
9191

9292
def testExceptionPickling(self):
93-
pickle.loads(pickle.dumps(github.GithubException("foo", "bar")))
93+
pickle.loads(pickle.dumps(github.GithubException("foo", "bar", None)))
9494

9595

9696
class SpecificExceptions(Framework.TestCase):
@@ -134,7 +134,9 @@ def exceed():
134134
res = self.g.search_code("jacquev6")
135135
res.get_page(0)
136136

137-
self.assertRaises(github.RateLimitExceededException, exceed)
137+
with self.assertRaises(github.RateLimitExceededException) as raised:
138+
exceed()
139+
self.assertEqual(raised.exception.headers.get("retry-after"), "60")
138140

139141
def testIncompletableObject(self):
140142
github.UserKey.UserKey.setCheckAfterInitFlag(False)

0 commit comments

Comments
 (0)