-
Notifications
You must be signed in to change notification settings - Fork 300
/
Copy pathexceptions.py
28 lines (24 loc) · 956 Bytes
/
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
from django.utils import six, encoding
from rest_framework.views import exception_handler as drf_exception_handler
from rest_framework_json_api.utils import format_value
def exception_handler(exc, context):
response = drf_exception_handler(exc, context)
errors = []
for field, error in response.data.items():
field = format_value(field)
pointer = '/data/attributes/{}'.format(field)
# see if they passed a dictionary to ValidationError manually
if isinstance(error, dict):
errors.append(error)
else:
for message in error:
errors.append({
'detail': message,
'source': {
'pointer': pointer,
},
'status': encoding.force_text(response.status_code),
})
context['view'].resource_name = 'errors'
response.data = errors
return response