View stacktrace error on 500 as json on django

Using django rest framework, I always make API calls through tests. But sometimes they fail and I am currently logging the HTML with a 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 other than html?

+3


source to share


1 answer


DRF only handles the following: (source: DRF Docs )

  • APIException subclasses thrown by REST.
  • Django Exception Http404.
  • Django PermissionDenied exception.

If you know what kind of view this is going on, one way to make it render API responses is to try / throw and raise it as an API exception instead of what you actually get.



Alternatively, 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) that handles more than standard exception types by default, using the EXCEPTION_HANDLER parameter for REST_FRAMEWORK

REST_FRAMEWORK = {
    'EXCEPTION_HANDLER': 'my_project.my_app.utils.custom_exception_handler'
}

      

+2


source







All Articles