diff --git a/tap_quickbooks/quickbooks/exceptions.py b/tap_quickbooks/quickbooks/exceptions.py index f990a96..77928e1 100644 --- a/tap_quickbooks/quickbooks/exceptions.py +++ b/tap_quickbooks/quickbooks/exceptions.py @@ -1,4 +1,25 @@ # pylint: disable=super-init-not-called +import requests + +def raise_for_invalid_credentials(resp): + def message_to_dict(input_string): + # Split the string by commas to separate each key-value pair + pairs = input_string.split("; ") + + # Create a dictionary by splitting each pair by '=' and using them as key-value + result_dict = {key: value for key, value in (pair.split("=") for pair in pairs)} + + return result_dict + + try: + response_dict_message = resp.json()["fault"]["error"][0]["message"] + response_dict = message_to_dict(response_dict_message) + if response_dict["statusCode"] == "403": + raise requests.HTTPError(f"[{response_dict['statusCode']}] Your credentials are invalid. Please check if you are using sandbox credentials to access production data and try again.",request=resp.request,response=resp) + except requests.exceptions.HTTPError as ex: + raise ex + except Exception: + pass class TapQuickbooksException(Exception): pass diff --git a/tap_quickbooks/quickbooks/rest.py b/tap_quickbooks/quickbooks/rest.py index 4d50545..6258d1f 100644 --- a/tap_quickbooks/quickbooks/rest.py +++ b/tap_quickbooks/quickbooks/rest.py @@ -4,7 +4,8 @@ import singer.utils as singer_utils from requests.exceptions import HTTPError -from tap_quickbooks.quickbooks.exceptions import TapQuickbooksException +from tap_quickbooks.quickbooks.exceptions import TapQuickbooksException, raise_for_invalid_credentials +from requests.models import Response LOGGER = singer.get_logger() @@ -73,7 +74,11 @@ def _query_recur( day_range, catalog_entry['stream']) retryable = True - except: + else: + raise_for_invalid_credentials(ex.response) + except HTTPError as http_ex: + raise http_ex + except Exception: raise ex if retryable: