|
@@ -1,3 +1,6 @@
|
|
|
+import json
|
|
|
+import yaml
|
|
|
+
|
|
|
from rest_framework import renderers
|
|
|
|
|
|
|
|
@@ -12,28 +15,10 @@ class PlainTextRenderer(renderers.BaseRenderer):
|
|
|
|
|
|
if response and response.exception:
|
|
|
response['Content-Type'] = 'text/plain'
|
|
|
-
|
|
|
try:
|
|
|
return data['detail']
|
|
|
- except (KeyError, TypeError):
|
|
|
- pass
|
|
|
-
|
|
|
- try:
|
|
|
- details = list(filter(None, [el.get('detail') for el in data]))
|
|
|
- if details:
|
|
|
- return ', '.join(details)
|
|
|
- except (TypeError, AttributeError):
|
|
|
- pass
|
|
|
-
|
|
|
- try:
|
|
|
- return '; '.join([f'{err.code}: {err}' for err in data])
|
|
|
- except (TypeError, AttributeError):
|
|
|
- pass
|
|
|
-
|
|
|
- raise ValueError('Expected response.data to be one of the following:\n'
|
|
|
- '- a dict with error details in response.data[\'detail\'],\n'
|
|
|
- '- a list with at least one element that has error details in element[\'detail\'];\n'
|
|
|
- '- a list with all elements being ErrorDetail instances;\n'
|
|
|
- 'but got %s:\n\n%s' % (type(response.data), response.data))
|
|
|
+ except:
|
|
|
+ data = json.loads(json.dumps(data)) # stringify exception objects in potentially nested data structure
|
|
|
+ return yaml.safe_dump(data, default_flow_style=False)
|
|
|
|
|
|
return data
|