Reputation: 687
Using django rest framework, I'm always making API calls through tests. But sometimes they fail and I am currently logging the HTML with the stacktrace to a file and then viewing it with a browser, but this is very annoying.
Is there a way to make it output json or anything else besides html?
Upvotes: 4
Views: 1078
Reputation: 1262
DRF only handles the following: (source: DRF docs)
If you know which view this is happening, one way of making it render API responses would be to try/except and raise it as an APIException instead of whatever you are actually getting.
Alternately you can also provide your own Exception Handler (see https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/views.py#L52 for the original) which handles more than the default 3 exception types, by using the EXCEPTION_HANDLER setting for REST_FRAMEWORK
REST_FRAMEWORK = {
'EXCEPTION_HANDLER': 'my_project.my_app.utils.custom_exception_handler'
}
Upvotes: 2