This repository was archived by the owner on Nov 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexceptions.py
More file actions
44 lines (38 loc) · 1.48 KB
/
exceptions.py
File metadata and controls
44 lines (38 loc) · 1.48 KB
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
import logging
from django.core.exceptions import ObjectDoesNotExist, ValidationError
from rest_framework import exceptions, views
logger = logging.getLogger(__name__)
def exception_handler(exc, context):
response = views.exception_handler(exc, context)
if response is None:
# From here, check for each different exceptions
if isinstance(exc, ValidationError):
# Usually thrown by ´is_valid(raise_exception=True)´
new_exc = exceptions.ParseError(str(exc))
elif isinstance(exc, ObjectDoesNotExist):
# Not found
new_exc = exceptions.NotFound(str(exc))
elif isinstance(exc, Exception):
# Unknown exception
new_exc = exceptions.APIException(str(exc))
else:
new_exc = exceptions.APIException('Unknown exception!')
response = views.exception_handler(new_exc, context)
if isinstance(response.data, list):
error_message = response.data
elif 'detail' in response.data:
error_message = response.data['detail']
else:
error_message = response.data
response.data = {
'exception': error_message,
}
# Check if it's an error 500
# TODO Find a better place to put this error
if response.status_code >= 500:
logger.error('Got error %d', response.status_code)
logger.error('Response:')
logger.error(response)
logger.error('Original error:')
logger.error(exc)
return response