Skip to content

Commit

Permalink
Updating logging levels (#201)
Browse files Browse the repository at this point in the history
* Updating logging levels

* fix lint

* rm workflow

* ignore exception

* more exception

* few more fixes

* fix pylint

* fix test

* add more exception

* remove 4xx logger

* remove 4xx logger

* add logging level

* rm logger

* rm
  • Loading branch information
ashwin1111 authored Jan 18, 2023
1 parent bc73590 commit 3262fdf
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 106 deletions.
18 changes: 0 additions & 18 deletions .github/workflows/pylint.yml

This file was deleted.

2 changes: 1 addition & 1 deletion apps/fyle/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def create_expense_objects(expenses: List[Dict], workspace_id: int):
eliminated_expenses.append(expense['id'])

if eliminated_expenses:
logger.error('Expenses with ids {} are not eligible for import'.format(eliminated_expenses))
logger.info('Expenses with ids {} are not eligible for import'.format(eliminated_expenses))

return expense_objects

Expand Down
106 changes: 68 additions & 38 deletions apps/mappings/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

from fyle.platform.exceptions import WrongParamsError

from xerosdk.exceptions import UnsuccessfulAuthentication, InvalidGrant

from apps.xero.utils import XeroConnector
from apps.tasks.models import Error
from apps.workspaces.models import XeroCredentials, FyleCredential, WorkspaceGeneralSettings
Expand Down Expand Up @@ -111,43 +113,35 @@ def upload_categories_to_fyle(workspace_id):
"""
Upload categories to Fyle
"""
try:
fyle_credentials: FyleCredential = FyleCredential.objects.get(workspace_id=workspace_id)
xero_credentials: XeroCredentials = XeroCredentials.get_active_xero_credentials(workspace_id)
general_settings = WorkspaceGeneralSettings.objects.filter(workspace_id=workspace_id).first()
platform = PlatformConnector(fyle_credentials)

category_map = get_all_categories_from_fyle(platform=platform)

xero_connection = XeroConnector(
credentials_object=xero_credentials,
workspace_id=workspace_id
)
platform.categories.sync()
xero_connection.sync_accounts()
fyle_credentials: FyleCredential = FyleCredential.objects.get(workspace_id=workspace_id)
xero_credentials: XeroCredentials = XeroCredentials.get_active_xero_credentials(workspace_id)
general_settings = WorkspaceGeneralSettings.objects.filter(workspace_id=workspace_id).first()
platform = PlatformConnector(fyle_credentials)

xero_attributes = DestinationAttribute.objects.filter(
workspace_id=workspace_id,
attribute_type='ACCOUNT',
detail__account_type__in=general_settings.charts_of_accounts
).all()
category_map = get_all_categories_from_fyle(platform=platform)

xero_attributes = remove_duplicates(xero_attributes)
xero_connection = XeroConnector(
credentials_object=xero_credentials,
workspace_id=workspace_id
)
platform.categories.sync()
xero_connection.sync_accounts()

fyle_payload: List[Dict] = create_fyle_categories_payload(xero_attributes, workspace_id, category_map)
xero_attributes = DestinationAttribute.objects.filter(
workspace_id=workspace_id,
attribute_type='ACCOUNT',
detail__account_type__in=general_settings.charts_of_accounts
).all()

if fyle_payload:
platform.categories.post_bulk(fyle_payload)
platform.categories.sync()
xero_attributes = remove_duplicates(xero_attributes)

return xero_attributes
fyle_payload: List[Dict] = create_fyle_categories_payload(xero_attributes, workspace_id, category_map)

except XeroCredentials.DoesNotExist:
logger.error(
'Xero Credentials not found for workspace_id %s',
workspace_id,
)
if fyle_payload:
platform.categories.post_bulk(fyle_payload)
platform.categories.sync()

return xero_attributes

def auto_create_category_mappings(workspace_id):
"""
Expand All @@ -164,6 +158,15 @@ def auto_create_category_mappings(workspace_id):

return []

except XeroCredentials.DoesNotExist:
logger.info(
'Xero Credentials not found for workspace_id %s',
workspace_id,
)

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

except WrongParamsError as exception:
logger.error(
'Error while creating categories workspace_id - %s in Fyle %s %s',
Expand Down Expand Up @@ -220,11 +223,13 @@ def async_auto_map_employees(workspace_id: int):
resolve_expense_attribute_errors(source_attribute_type='EMPLOYEE', workspace_id=workspace_id)

except XeroCredentials.DoesNotExist:
logger.error(
logger.info(
'Xero Credentials not found for workspace_id %s',
workspace_id,
)

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

def schedule_auto_map_employees(employee_mapping_preference: str, workspace_id: str):
if employee_mapping_preference:
Expand Down Expand Up @@ -334,12 +339,18 @@ def auto_create_cost_center_mappings(workspace_id: int):

post_cost_centers_in_batches(platform, workspace_id, mapping_setting.destination_field)

except XeroCredentials.DoesNotExist:
logger.info('Xero credentials does not exist for workspace_id - %s', workspace_id)

except WrongParamsError as exception:
logger.error(
'Error while creating cost centers workspace_id - %s in Fyle %s %s',
workspace_id, exception.message, {'error': exception.response}
)

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

except Exception:
error = traceback.format_exc()
error = {
Expand Down Expand Up @@ -440,12 +451,18 @@ def auto_create_project_mappings(workspace_id: int):

post_projects_in_batches(platform, workspace_id, mapping_setting.destination_field)

except XeroCredentials.DoesNotExist:
logger.info('Xero credentials does not exist for workspace_id - %s', workspace_id)

except WrongParamsError as exception:
logger.error(
'Error while creating projects workspace_id - %s in Fyle %s %s',
workspace_id, exception.message, {'error': exception.response}
)

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

except Exception:
error = traceback.format_exc()
error = {
Expand Down Expand Up @@ -603,12 +620,19 @@ def async_auto_create_custom_field_mappings(workspace_id: str):
).all()

for mapping_setting in mapping_settings:
if mapping_setting.import_to_fyle:
sync_xero_attributes(mapping_setting.destination_field, workspace_id)
auto_create_expense_fields_mappings(
workspace_id, mapping_setting.destination_field, mapping_setting.source_field,
mapping_setting.source_placeholder
)
try:
if mapping_setting.import_to_fyle:
sync_xero_attributes(mapping_setting.destination_field, workspace_id)
auto_create_expense_fields_mappings(
workspace_id, mapping_setting.destination_field, mapping_setting.source_field,
mapping_setting.source_placeholder
)

except XeroCredentials.DoesNotExist:
logger.info('Xero credentials does not exist for workspace_id - %s', workspace_id)

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


def schedule_fyle_attributes_creation(workspace_id: int):
Expand Down Expand Up @@ -691,18 +715,24 @@ def auto_create_tax_codes_mappings(workspace_id: int):

upload_tax_groups_to_fyle(platform, workspace_id)

except XeroCredentials.DoesNotExist:
logger.info('Xero credentials does not exist for workspace_id - %s', workspace_id)

except WrongParamsError as exception:
logger.error(
'Error while creating tax groups workspace_id - %s in Fyle %s %s',
workspace_id, exception.message, {'error': exception.response}
)

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

except Exception:
error = traceback.format_exc()
error = {
'error': error
}
logger.error(
logger.exception(
'Error while creating tax groups workspace_id - %s error: %s',
workspace_id, error
)
Expand Down
9 changes: 7 additions & 2 deletions apps/mappings/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

from fyle_xero_api.utils import assert_valid

from xerosdk.exceptions import UnsuccessfulAuthentication

from .serializers import TenantMappingSerializer, GeneralMappingSerializer
from .models import TenantMapping, GeneralMapping
from apps.workspaces.models import XeroCredentials
Expand Down Expand Up @@ -54,8 +56,11 @@ def post(self, request, *args, **kwargs):
tenant_mapping.connection_id = connection[0]['id']
tenant_mapping.save()

except:
logger.error('Error while fetching company information')
except UnsuccessfulAuthentication:
logger.info('Xero refresh token is invalid for workspace_id - %s', kwargs['workspace_id'])

except Exception:
logger.info('Error while fetching company information')

return Response(
data=self.serializer_class(tenant_mapping_object).data,
Expand Down
11 changes: 3 additions & 8 deletions apps/workspaces/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ def post(self, request, **kwargs):
xero_credentials.country = company_info['CountryCode']
xero_credentials.save()

except xero_exc.WrongParamsError as exception:
logger.error(exception.response)
except (xero_exc.WrongParamsError, xero_exc.UnsuccessfulAuthentication) as exception:
logger.info(exception.response)

if workspace.onboarding_state == 'CONNECTION':
workspace.onboarding_state = 'EXPORT_SETTINGS'
Expand All @@ -319,12 +319,7 @@ def post(self, request, **kwargs):
data=XeroCredentialSerializer(xero_credentials).data,
status=status.HTTP_200_OK
)
except xero_exc.InvalidClientError as e:
return Response(
json.loads(e.response),
status=status.HTTP_400_BAD_REQUEST
)
except xero_exc.InvalidGrant as e:
except (xero_exc.InvalidClientError, xero_exc.InvalidGrant, xero_exc.UnsuccessfulAuthentication) as e:
return Response(
json.loads(e.response),
status=status.HTTP_400_BAD_REQUEST
Expand Down
Loading

0 comments on commit 3262fdf

Please sign in to comment.