-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexceptions.py
101 lines (84 loc) · 3.68 KB
/
exceptions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import logging
from rest_framework.response import Response
from rest_framework.views import status
from rest_framework.exceptions import ValidationError
from fyle.platform.exceptions import NoPrivilegeError
from fyle.platform.exceptions import InvalidTokenError as FyleInvalidTokenError
from apps.fyle.models import ExpenseGroup
from apps.mappings.models import GeneralMapping
from apps.workspaces.models import FyleCredential, SageIntacctCredential, Workspace, Configuration
logger = logging.getLogger(__name__)
logger.level = logging.INFO
def handle_view_exceptions() -> callable:
"""
Decorator to handle exceptions in views
"""
def decorator(func: callable) -> callable:
def new_fn(*args, **kwargs) -> callable:
try:
return func(*args, **kwargs)
except ExpenseGroup.DoesNotExist:
return Response(
data={'message': 'Expense group not found'},
status=status.HTTP_400_BAD_REQUEST
)
except FyleCredential.DoesNotExist:
return Response(
data={'message': 'Fyle credentials not found in workspace'},
status=status.HTTP_400_BAD_REQUEST
)
except FyleInvalidTokenError as exception:
logger.info(
'Fyle token expired workspace_id - %s %s',
kwargs['workspace_id'],
{'error': exception.response}
)
return Response(
data={'message': 'Fyle token expired workspace_id'},
status=status.HTTP_400_BAD_REQUEST
)
except GeneralMapping.DoesNotExist:
return Response(
{'message': 'General mappings do not exist for the workspace'},
status=status.HTTP_400_BAD_REQUEST
)
except NoPrivilegeError as exception:
logger.info(
'Invalid Fyle Credentials / Admin is disabled for workspace_id%s %s',
kwargs['workspace_id'],
{'error': exception.response}
)
return Response(
data={'message': 'Invalid Fyle Credentials / Admin is disabled'},
status=status.HTTP_400_BAD_REQUEST
)
except Workspace.DoesNotExist:
return Response(
data={'message': 'Workspace with this id does not exist'},
status=status.HTTP_400_BAD_REQUEST
)
except Configuration.DoesNotExist:
return Response(
data={'message': 'Configuration does not exist in workspace'},
status=status.HTTP_400_BAD_REQUEST
)
except SageIntacctCredential.DoesNotExist:
logger.info('SageIntacct credentials not found in workspace')
return Response(
data={'message': 'SageIntacct credentials not found in workspace'},
status=status.HTTP_400_BAD_REQUEST
)
except ValidationError as e:
logger.exception(e)
return Response(
{"message": e.detail},
status=status.HTTP_400_BAD_REQUEST
)
except Exception as exception:
logger.exception(exception)
return Response(
data={'message': 'An unhandled error has occurred, please re-try later'},
status=status.HTTP_400_BAD_REQUEST
)
return new_fn
return decorator