title: Include request data in Django Rest Framework custom exception handler response data
body: <p><strong>Tech used:</strong></p>
  
  <p><a href="http://www.django-rest-framework.org" rel="nofollow">http://www.django-rest-framework.org</a></p>
  
  <p><strong>Exceptions:</strong> <a href="http://www.django-rest-framework.org/api-guide/exceptions/" rel="nofollow">http://www.django-rest-framework.org/api-guide/exceptions/</a></p>
  
  <p>Included rest_framework default example in custom exceptions.py file:</p>
  
  <pre><code>from rest_framework.views import exception_handler
  
  import sys
  
  def custom_exception_handler(exc, context=None):
  
      # Call REST framework's default exception handler first,
      # to get the standard error response.
      response = exception_handler(exc)
  
      # Now add the HTTP status code to the response and rename detail to error
      if response is not None:
          response.data['status_code'] = response.status_code
          response.data['request'] = request
          response.data['error'] = response.data.get('detail')
          del response.data['detail']
  
      return response
  </code></pre>
  
  <p>This sends basic error info like "Http404" etc, but no request data, like ip address, etc.</p>
  
  <p>Best way to add my request into the response? Thanks in advance.</p>
  
  <p><strong>UPDATE (and solved):</strong></p>
  
  <p>So, I was initially trying to solve this using DjangoRestFramework 2.4.x, but that version doesn't have the request or context data options for the custom exception handler. Upgrading to 3.1.3 made it easy to add the data into the response. New code now looks like this (using version 3.1.3):</p>
  
  <pre><code>def custom_exception_handler(exc, request):
  
  # Call REST framework's default exception handler first,
  # to get the standard error response.
  response = exception_handler(exc, request)
  
  # Send error to rollbar
  rollbar.report_exc_info(sys.exc_info(), request)
  
  # Now add the HTTP status code to the response and rename detail to error
  if response is not None:
      response.data['status_code'] = response.status_code
      response.data['error'] = response.data.get('detail')
      del response.data['detail']
  
  return response
  </code></pre>