1
- from django .utils import encoding
1
+ import inspect
2
+ from django .utils import six , encoding
2
3
from django .utils .translation import ugettext_lazy as _
3
- from rest_framework import status
4
- from rest_framework .exceptions import APIException
4
+ from rest_framework import status , exceptions
5
5
from rest_framework .views import exception_handler as drf_exception_handler
6
6
7
7
from rest_framework_json_api .utils import format_value
@@ -32,11 +32,17 @@ def exception_handler(exc, context):
32
32
# see if they passed a dictionary to ValidationError manually
33
33
if isinstance (error , dict ):
34
34
errors .append (error )
35
- # or a string in case of AuthenticationError
36
- elif isinstance (error , str ):
37
- # An error MUST be a JSON object in JSON API spec
35
+ elif isinstance (error , six .string_types ):
36
+ classes = inspect .getmembers (exceptions , inspect .isclass )
37
+ # DRF sets the `field` to 'detail' for its own exceptions
38
+ if isinstance (exc , tuple (x [1 ] for x in classes )):
39
+ pointer = '/data'
38
40
errors .append ({
39
- 'detail' : error
41
+ 'detail' : error ,
42
+ 'source' : {
43
+ 'pointer' : pointer ,
44
+ },
45
+ 'status' : encoding .force_text (response .status_code ),
40
46
})
41
47
elif isinstance (error , list ):
42
48
for message in error :
@@ -49,7 +55,7 @@ def exception_handler(exc, context):
49
55
})
50
56
else :
51
57
errors .append ({
52
- 'detail' : message ,
58
+ 'detail' : error ,
53
59
'source' : {
54
60
'pointer' : pointer ,
55
61
},
@@ -62,6 +68,7 @@ def exception_handler(exc, context):
62
68
return response
63
69
64
70
65
- class Conflict (APIException ):
71
+ class Conflict (exceptions . APIException ):
66
72
status_code = status .HTTP_409_CONFLICT
67
73
default_detail = _ ('Conflict.' )
74
+
0 commit comments