diff --git a/tap_quickbooks/quickbooks/rest_reports.py b/tap_quickbooks/quickbooks/rest_reports.py index a50b300..0c5f4e1 100644 --- a/tap_quickbooks/quickbooks/rest_reports.py +++ b/tap_quickbooks/quickbooks/rest_reports.py @@ -16,6 +16,8 @@ def is_fatal_code(e: requests.exceptions.RequestException) -> bool: instead of attemtping to backoff.''' return 400 <= e.response.status_code < 500 and e.response.status_code != 429 +class RetriableException(Exception): + pass @attr.s class QuickbooksStream: @@ -31,7 +33,9 @@ def _get_abs_path(self, path: str) -> str: giveup=is_fatal_code) @backoff.on_exception(backoff.fibo, (requests.exceptions.ConnectionError, - requests.exceptions.Timeout), + requests.exceptions.Timeout, + RetriableException + ), max_tries=5) def _get(self, report_entity: str, params: Optional[Dict] = None) -> Dict: '''Constructs a standard way of making @@ -49,7 +53,17 @@ def _get(self, report_entity: str, params: Optional[Dict] = None) -> Dict: # Wait 60 seconds before retrying the request. time.sleep(60) response.raise_for_status() - return response.json() + + # handle random empty responses or not valid json responses + try: + res_json = response.json() + except: + raise RetriableException(f"Invalid json response: {response.text} ") + + if res_json == None: + raise RetriableException(f"Empty response returned {response.text} ") + + return res_json def _convert_string_value_to_float(self, value: str) -> float: '''Safely converts string values to floats.'''