title: How to capture exception in django rest framework with django middleware?
body: <p>Django middleware have a <code>process_exception</code> hook which can be used to capture exception and handler.</p>
  
  <p>But there is some  problem while using Django restframe work</p>
  
  <pre><code>class ExceptionHandler(MiddlewareMixin):
  
      @staticmethod
      def process_exception(request, exception):
  
          if isinstance(exception, ValidationError):
              return Response(data=exception.messages, status=status.HTTP_400_BAD_REQUEST)
  </code></pre>
  
  <p>For example, I try to use above middleware to capture the <code>ValidationError</code> and return HTTP 400</p>
  
  <p>But it will not work and raise below error</p>
  
  <pre><code>AssertionError: .accepted_renderer not set on Response
  </code></pre>
  
  <p>It turns out that the rest-framework view layer will add a <code>.accepted_renderer</code> to  the  response.</p>
  
  <p>If I handle the exception outside view. This attribute will be  missed and cause another exception.</p>
  
  <p>So my question is: Is it wrong to handle exception  in middleware when using django rest-framework?</p>
  
  <p>What is the correct way to do ?</p>