Skip to content

Commit 778682b

Browse files
luandy64harshpatel4_cresthpatel41
authored
Raise an exception instead of swallowing an error (#126)
* Craft the perfect storm to get an AttributeError from raise_for_error * removed the code for checking content length Co-authored-by: harshpatel4_crest <[email protected]> Co-authored-by: Harsh <[email protected]>
1 parent a513fdd commit 778682b

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

tap_github/__init__.py

+3-12
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,6 @@ def get_bookmark(state, repo, stream_name, bookmark_key, start_date):
165165
return None
166166

167167
def raise_for_error(resp, source):
168-
169-
content_length = len(resp.content)
170-
if content_length == 0:
171-
# There is nothing we can do here since Github has neither sent
172-
# us a 2xx response nor a response content.
173-
return
174-
175168
error_code = resp.status_code
176169
try:
177170
response_json = resp.json()
@@ -212,11 +205,9 @@ def authed_get(source, url, headers={}):
212205
resp = session.request(method='get', url=url)
213206
if resp.status_code != 200:
214207
raise_for_error(resp, source)
215-
return None
216-
else:
217-
timer.tags[metrics.Tag.http_status_code] = resp.status_code
218-
rate_throttling(resp)
219-
return resp
208+
timer.tags[metrics.Tag.http_status_code] = resp.status_code
209+
rate_throttling(resp)
210+
return resp
220211

221212
def authed_get_all_pages(source, url, headers={}):
222213
while True:

tests/unittests/test_exception_handling.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
import requests
55

66
class Mockresponse:
7-
def __init__(self, status_code, json, raise_error, headers={'X-RateLimit-Remaining': 1}, text=None):
7+
def __init__(self, status_code, json, raise_error, headers={'X-RateLimit-Remaining': 1}, text=None, content=None):
88
self.status_code = status_code
99
self.raise_error = raise_error
1010
self.text = json
1111
self.headers = headers
12-
self.content = "github"
12+
self.content = content if content is not None else 'github'
1313

1414
def raise_for_status(self):
1515
if not self.raise_error:
@@ -20,11 +20,19 @@ def raise_for_status(self):
2020
def json(self):
2121
return self.text
2222

23-
def get_response(status_code, json={}, raise_error=False):
24-
return Mockresponse(status_code, json, raise_error)
23+
def get_response(status_code, json={}, raise_error=False, content=None):
24+
return Mockresponse(status_code, json, raise_error, content=content)
2525

2626
@mock.patch("requests.Session.request")
2727
class TestExceptionHandling(unittest.TestCase):
28+
def test_zero_content_length(self, mocked_request):
29+
mocked_request.return_value = get_response(400, raise_error = True, content='')
30+
31+
try:
32+
tap_github.authed_get("", "")
33+
except tap_github.BadRequestException as e:
34+
self.assertEquals(str(e), "HTTP-error-code: 400, Error: The request is missing or has a bad parameter.")
35+
2836
def test_400_error(self, mocked_request):
2937
mocked_request.return_value = get_response(400, raise_error = True)
3038

0 commit comments

Comments
 (0)