|
| 1 | +import logging |
| 2 | + |
| 3 | +from fyle.platform.exceptions import NoPrivilegeError, RetryException, InvalidTokenError as FyleInvalidTokenError |
| 4 | +from rest_framework.response import Response |
| 5 | +from rest_framework.views import status |
| 6 | + |
| 7 | +from apps.workspaces.models import FyleCredential, Workspace, ExportSettings, AdvancedSetting |
| 8 | +from apps.tasks.models import AccountingExport |
| 9 | + |
| 10 | +logger = logging.getLogger(__name__) |
| 11 | +logger.level = logging.INFO |
| 12 | + |
| 13 | + |
| 14 | +def handle_view_exceptions(): |
| 15 | + def decorator(func): |
| 16 | + def new_fn(*args, **kwargs): |
| 17 | + try: |
| 18 | + return func(*args, **kwargs) |
| 19 | + except AccountingExport.DoesNotExist: |
| 20 | + return Response(data={'message': 'AccountingExport not found'}, status=status.HTTP_400_BAD_REQUEST) |
| 21 | + |
| 22 | + except FyleCredential.DoesNotExist: |
| 23 | + return Response(data={'message': 'Fyle credentials not found in workspace'}, status=status.HTTP_400_BAD_REQUEST) |
| 24 | + |
| 25 | + except FyleInvalidTokenError as exception: |
| 26 | + logger.info('Fyle token expired workspace_id - %s %s', kwargs['workspace_id'], {'error': exception.response}) |
| 27 | + return Response(data={'message': 'Fyle token expired workspace_id'}, status=status.HTTP_400_BAD_REQUEST) |
| 28 | + |
| 29 | + except NoPrivilegeError as exception: |
| 30 | + logger.info('Invalid Fyle Credentials / Admin is disabled for workspace_id%s %s', kwargs['workspace_id'], {'error': exception.response}) |
| 31 | + return Response(data={'message': 'Invalid Fyle Credentials / Admin is disabled'}, status=status.HTTP_400_BAD_REQUEST) |
| 32 | + |
| 33 | + except RetryException: |
| 34 | + logger.info('Fyle Retry Exception for workspace_id %s', kwargs['workspace_id']) |
| 35 | + return Response(data={'message': 'Fyle API rate limit exceeded'}, status=status.HTTP_400_BAD_REQUEST) |
| 36 | + |
| 37 | + except Workspace.DoesNotExist: |
| 38 | + return Response(data={'message': 'Workspace with this id does not exist'}, status=status.HTTP_400_BAD_REQUEST) |
| 39 | + |
| 40 | + except AdvancedSetting.DoesNotExist: |
| 41 | + return Response(data={'message': 'Advanced Settings does not exist in workspace'}, status=status.HTTP_400_BAD_REQUEST) |
| 42 | + |
| 43 | + except ExportSettings.DoesNotExist: |
| 44 | + return Response({'message': 'Export Settings does not exist in workspace'}, status=status.HTTP_400_BAD_REQUEST) |
| 45 | + |
| 46 | + except Exception as exception: |
| 47 | + logger.exception(exception) |
| 48 | + return Response(data={'message': 'An unhandled error has occurred, please re-try later'}, status=status.HTTP_400_BAD_REQUEST) |
| 49 | + |
| 50 | + return new_fn |
| 51 | + |
| 52 | + return decorator |
0 commit comments