Skip to content

Commit

Permalink
add handling for empty or non valid json responses (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
keyn4 authored Jun 20, 2024
1 parent 7410088 commit 4ffc40d
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions tap_quickbooks/quickbooks/rest_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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.'''
Expand Down

0 comments on commit 4ffc40d

Please sign in to comment.