Skip to content

Commit

Permalink
[TDL-22316] : Handle ChunkEncodingError & JSONDecodeError (#36)
Browse files Browse the repository at this point in the history
* Changes:
1) Added logic to handle ChunkedEncodingError and JSONDecodeError.
2) Added necessary unit test case.

* Changes:
1) Improved exception throwing and handling logic.
2) Added backoff for ConnectionResetError.
3) Added unit test case for ConnectionResetError.
  • Loading branch information
shantanu73 authored Mar 23, 2023
1 parent 69c9ff1 commit 4f85b5f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
8 changes: 7 additions & 1 deletion tap_activecampaign/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ def should_retry_error(exception):
# Tap raises Exception: ConnectionResetError(104, 'Connection reset by peer'). That's why we are retrying this error also.
# Reference: https://app.circleci.com/pipelines/github/singer-io/tap-activecampaign/554/workflows/d448258e-20df-4e66-b2aa-bc8bd1f08912/jobs/558
return True
elif type(exception) == ConnectionResetError:
return True
else:
return False

Expand Down Expand Up @@ -235,7 +237,11 @@ def request(self, method, path=None, url=None, api_version=None, **kwargs):
except Exception as err:
LOGGER.error('{}'.format(err))
LOGGER.error('response content: {}'.format(response.content))
raise Exception(err)
if response.content != b"":
raise err

# Handling empty response b'' given by ActiveCampaign APIs
response_json = {}

return response_json

Expand Down
42 changes: 42 additions & 0 deletions tests/unittests/test_error_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,21 @@ def test_request_with_handling_for_500_exception_handling(self, mocked_request,
self.assertEqual(str(e.exception), expected_error_message)


@patch("time.sleep")
@patch("tap_activecampaign.client.ActiveCampaignClient.check_api_token")
@patch("requests.Session.request", return_value=Mockresponse("", 200, content=b""))
def test_request_with_handling_for_empty_content(self, mocked_request, mock_api_token, mock_sleep):
"""
Test that `request` method gives empty json `{}` response when content is empty for a 200 response.
"""
_client = client.ActiveCampaignClient('dummy_url', 'dummy_token')

response = _client.request("base_url")

# Verifying the empty response
self.assertEqual({}, response)


class TestActiveCampaignErrorhandlingForCheckApiTokenMethod(unittest.TestCase):

@patch("requests.Session.get", side_effect=mock_send_400)
Expand Down Expand Up @@ -398,6 +413,19 @@ def test_enter_method_handle_connection_reset_exception(self, mocked_request, mo
# Verify that requests.Session.get called 5 times
self.assertEqual(mocked_request.call_count, 5)

@patch("requests.Session.get", side_effect=ConnectionResetError(104, 'Connection reset by peer'))
def test_enter_method_handle_connection_reset_error(self, mocked_request, mock_sleep):
"""
Test that `__enter__` method retry ConnectionResetError(104) with Exception 5 times.
"""
_client = client.ActiveCampaignClient('dummy_url', 'dummy_token')

with self.assertRaises(ConnectionResetError) as e:
_client.__enter__()

# Verify that requests.Session.get called 5 times
self.assertEqual(mocked_request.call_count, 5)

@patch("tap_activecampaign.client.ActiveCampaignClient.check_api_token")
@patch("requests.Session.request", side_effect=mock_send_429)
def test_request_method_handle_429_exception(self, mocked_request, mock_api_token, mock_sleep):
Expand Down Expand Up @@ -495,3 +523,17 @@ def test_request_method_handle_connection_reset_exception(self, mocked_request,

# Verify that requests.Session.request called 5 times
self.assertEqual(mocked_request.call_count, 5)

@patch("tap_activecampaign.client.ActiveCampaignClient.check_api_token")
@patch("requests.Session.request", side_effect=ConnectionResetError(104, 'Connection reset by peer'))
def test_request_method_handle_connection_reset_error(self, mocked_request, mock_api_token, mock_sleep):
"""
Test that `request` method retry ConnectionResetError(104) with Exception 5 times.
"""
_client = client.ActiveCampaignClient('dummy_url', 'dummy_token')

with self.assertRaises(ConnectionResetError) as e:
_client.request("base_url")

# Verify that requests.Session.request called 5 times
self.assertEqual(mocked_request.call_count, 5)

0 comments on commit 4f85b5f

Please sign in to comment.