Skip to content

Commit

Permalink
Handle 5xx error (#226)
Browse files Browse the repository at this point in the history
* Comments Resolved

* Code coverage improved
  • Loading branch information
ruuushhh authored Apr 12, 2023
1 parent d3066d6 commit 365eb71
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
21 changes: 20 additions & 1 deletion apps/mappings/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from fyle_accounting_mappings.models import Mapping, MappingSetting, DestinationAttribute, ExpenseAttribute

from fyle.platform.exceptions import WrongParamsError, InvalidTokenError as FyleInvalidTokenError
from fyle.platform.exceptions import WrongParamsError, InternalServerError, InvalidTokenError as FyleInvalidTokenError

from xerosdk.exceptions import UnsuccessfulAuthentication, InvalidGrant

Expand Down Expand Up @@ -174,6 +174,10 @@ def auto_create_category_mappings(workspace_id):
'Error while creating categories workspace_id - %s in Fyle %s %s',
workspace_id, exception.message, {'error': exception.response}
)

except InternalServerError:
logger.error('Internal server error while importing to Fyle')

except Exception:
error = traceback.format_exc()
error = {
Expand Down Expand Up @@ -210,6 +214,9 @@ def async_auto_map_employees(workspace_id: int):
except FyleInvalidTokenError:
logger.info('Invalid Token for Fyle')

except InternalServerError:
logger.error('Internal server error while importing to Fyle')

except (UnsuccessfulAuthentication, InvalidGrant):
logger.info('Xero refresh token is invalid for workspace_id - %s', workspace_id)

Expand Down Expand Up @@ -333,6 +340,9 @@ def auto_create_cost_center_mappings(workspace_id: int):
workspace_id, exception.message, {'error': exception.response}
)

except InternalServerError:
logger.error('Internal server error while importing to Fyle')

except (UnsuccessfulAuthentication, InvalidGrant):
logger.info('Xero refresh token is invalid for workspace_id - %s', workspace_id)

Expand Down Expand Up @@ -449,6 +459,9 @@ def auto_create_project_mappings(workspace_id: int):
workspace_id, exception.message, {'error': exception.response}
)

except InternalServerError:
logger.error('Internal server error while importing to Fyle')

except (UnsuccessfulAuthentication, InvalidGrant):
logger.info('Xero refresh token is invalid for workspace_id - %s', workspace_id)

Expand Down Expand Up @@ -572,6 +585,9 @@ def auto_create_expense_fields_mappings(workspace_id: int, xero_attribute_type:
'Error while creating %s workspace_id - %s in Fyle %s %s',
fyle_attribute_type, workspace_id, exception.message, {'error': exception.response}
)

except InternalServerError:
logger.error('Internal server error while importing to Fyle')

except FyleInvalidTokenError:
logger.info('Invalid Token for Fyle')
Expand Down Expand Up @@ -701,6 +717,9 @@ def auto_create_tax_codes_mappings(workspace_id: int):
workspace_id, exception.message, {'error': exception.response}
)

except InternalServerError:
logger.error('Internal server error while importing to Fyle')

except (UnsuccessfulAuthentication, InvalidGrant):
logger.info('Xero refresh token is invalid for workspace_id - %s', workspace_id)

Expand Down
20 changes: 19 additions & 1 deletion tests/test_mappings/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Mapping, MappingSetting, EmployeeMapping
from apps.mappings.tasks import *
from fyle_integrations_platform_connector import PlatformConnector
from fyle.platform.exceptions import InvalidTokenError as FyleInvalidTokenError
from fyle.platform.exceptions import InvalidTokenError as FyleInvalidTokenError, InternalServerError
from ..test_xero.fixtures import data as xero_data
from ..test_fyle.fixtures import data as fyle_data
from .fixtures import data
Expand Down Expand Up @@ -62,6 +62,9 @@ def test_auto_create_tax_codes_mappings(db, mocker):
mock_call.side_effect = FyleInvalidTokenError(msg='Invalid Token for Fyle', response="Invalid Token for Fyle")
response = auto_create_tax_codes_mappings(workspace_id=workspace_id)

mock_call.side_effect = InternalServerError(msg='Internal server error while importing to Fyle', response="Internal server error while importing to Fyle")
response = auto_create_tax_codes_mappings(workspace_id=workspace_id)

def test_schedule_tax_groups_creation(db):
workspace_id = 1
schedule_tax_groups_creation(import_tax_codes=True, workspace_id=workspace_id)
Expand Down Expand Up @@ -120,6 +123,9 @@ def test_auto_create_project_mappings(db, mocker):
mock_call.side_effect = FyleInvalidTokenError(msg='Invalid Token for Fyle', response="Invalid Token for Fyle")
response = auto_create_project_mappings(workspace_id=workspace_id)

mock_call.side_effect = InternalServerError(msg='Internal server error while importing to Fyle', response="Internal server error while importing to Fyle")
response = auto_create_project_mappings(workspace_id=workspace_id)

mock_call.side_effect = Exception()
response = auto_create_project_mappings(workspace_id=workspace_id)

Expand Down Expand Up @@ -215,6 +221,9 @@ def test_auto_create_category_mappings(db, mocker):
mock_call.side_effect = FyleInvalidTokenError(msg='Invalid Token for Fyle', response="Invalid Token for Fyle")
response = auto_create_category_mappings(workspace_id=workspace_id)

mock_call.side_effect = InternalServerError(msg='Internal server error while importing to Fyle', response="Internal server error while importing to Fyle")
response = auto_create_category_mappings(workspace_id=workspace_id)

fyle_credentials = FyleCredential.objects.get(workspace_id=workspace_id)
fyle_credentials.delete()

Expand Down Expand Up @@ -255,6 +264,9 @@ def test_async_auto_map_employees(mocker, db):
mock_call.side_effect = UnsuccessfulAuthentication(msg='Auth error')
async_auto_map_employees(workspace_id=workspace_id)

mock_call.side_effect = InternalServerError(msg='Internal server error while importing to Fyle')
async_auto_map_employees(workspace_id=workspace_id)

qbo_credentials = XeroCredentials.objects.get(workspace_id=workspace_id)
qbo_credentials.delete()

Expand Down Expand Up @@ -319,6 +331,9 @@ def test_auto_create_cost_center_mappings(db, mocker, create_mapping_setting):
mock_call.side_effect = FyleInvalidTokenError(msg='Invalid Token for Fyle', response="Inalid Token for Fyle")
response = auto_create_cost_center_mappings(workspace_id=workspace_id)

mock_call.side_effect = InternalServerError(msg='Internal server error while importing to Fyle', response="Internal server error while importing to Fyle")
response = auto_create_cost_center_mappings(workspace_id=workspace_id)

fyle_credentials = FyleCredential.objects.get(workspace_id=workspace_id)
fyle_credentials.delete()

Expand Down Expand Up @@ -423,6 +438,9 @@ def test_auto_create_expense_fields_mappings(db, mocker, create_mapping_setting)
mock_call.side_effect = FyleInvalidTokenError(msg='Invalid Token for Fyle', response="Invalid Token for Fyle")
auto_create_expense_fields_mappings(workspace_id, 'COST_CENTER', 'TESTING_THIS')

mock_call.side_effect = InternalServerError(msg='Internal server error while importing to Fyle', response="Internal server error while importing to Fyle")
auto_create_expense_fields_mappings(workspace_id, 'COST_CENTER', 'TESTING_THIS')

mock_call.side_effect = Exception()
auto_create_expense_fields_mappings(workspace_id, 'COST_CENTER', 'TESTING_THIS')

Expand Down

0 comments on commit 365eb71

Please sign in to comment.