diff --git a/apps/sage300/utils.py b/apps/sage300/utils.py index dfa08b25..4c2e878d 100644 --- a/apps/sage300/utils.py +++ b/apps/sage300/utils.py @@ -7,6 +7,14 @@ from apps.mappings.models import Version from apps.mappings.exceptions import handle_import_exceptions +from sage_desktop_sdk.exceptions import ( + UserAccountLocked, + InvalidUserCredentials, + InvalidWebApiClientCredentials, + WebApiClientLocked, + SageDesktopSDKError +) + logger = logging.getLogger(__name__) logger.level = logging.INFO @@ -22,14 +30,16 @@ def __init__(self, credentials_object: Sage300Credential, workspace_id: int): :param credentials_object: Sage300Credential instance containing API credentials :param workspace_id: ID of the workspace """ - - self.connection = SageDesktopSDK( - api_key=credentials_object.api_key, - api_secret=credentials_object.api_secret, - user_name=credentials_object.username, - password=credentials_object.password, - indentifier=credentials_object.identifier - ) + try: + self.connection = SageDesktopSDK( + api_key=credentials_object.api_key, + api_secret=credentials_object.api_secret, + user_name=credentials_object.username, + password=credentials_object.password, + indentifier=credentials_object.identifier + ) + except (InvalidUserCredentials, InvalidWebApiClientCredentials, UserAccountLocked, WebApiClientLocked, SageDesktopSDKError) as e: + logger.error(f'Error while connecting to Sage300: {e}') self.workspace_id = workspace_id diff --git a/sage_desktop_sdk/core/client.py b/sage_desktop_sdk/core/client.py index 92fff4a8..b0ac0eba 100644 --- a/sage_desktop_sdk/core/client.py +++ b/sage_desktop_sdk/core/client.py @@ -60,31 +60,35 @@ def update_cookie(self, api_key: str, api_secret: str): 'Content-type': 'application/json', } - api_data = json.dumps({ - "ApiKey": api_key, - "ApiSecret": api_secret, - "Password": self.__user_password, + api_data = json.dumps({ + "ApiKey": api_key, + "ApiSecret": api_secret, + "Password": self.__user_password, "Username": self.__user_id }) authentication_url = self.__api_url + '/Api/Security/V3/Session.svc/authenticate' result = requests.request("POST", url=authentication_url, headers=request_header, data=api_data) - response = json.loads(result.text) + try: + response = json.loads(result.text) - if response['Result'] == 5: - self.__cookie = result.headers.get('Set-Cookie') + if response['Result'] == 5: + self.__cookie = result.headers.get('Set-Cookie') - if response['Result'] == 1: - raise InvalidUserCredentials('Invalid User Credentials') + if response['Result'] == 1: + raise InvalidUserCredentials('Invalid User Credentials') - if response['Result'] == 2: - raise InvalidWebApiClientCredentials('Invalid Webapp Client') + if response['Result'] == 2: + raise InvalidWebApiClientCredentials('Invalid Webapp Client') - if response['Result'] == 3: - raise UserAccountLocked('User Account Locked') + if response['Result'] == 3: + raise UserAccountLocked('User Account Locked') - if response['Result'] == 4: - raise WebApiClientLocked('Web API client Locked') + if response['Result'] == 4: + raise WebApiClientLocked('Web API client Locked') + + except Exception as e: + raise SageDesktopSDKError("Error while connecting with hh2 | {0}".format(e)) def _query_get_all_generator(self, url: str, is_paginated: bool = False) -> Generator[Dict, None, None]: """