technical_response.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. # -*- coding: utf-8 -*-
  2. import threading
  3. import six
  4. from django.core.handlers.wsgi import WSGIHandler
  5. tld = threading.local()
  6. tld.wsgi_tb = None
  7. def null_technical_500_response(request, exc_type, exc_value, tb, status_code=500):
  8. """
  9. Alternative function for django.views.debug.technical_500_response.
  10. Django's convert_exception_to_response() wrapper is called on each 'Middleware' object to avoid
  11. leaking exceptions. If an uncaught exception is raised, the wrapper calls technical_500_response()
  12. to create a response for django's debug view.
  13. Runserver_plus overrides the django debug view's technical_500_response() function to allow for
  14. an enhanced WSGI debugger view to be displayed. However, because Django calls
  15. convert_exception_to_response() on each object in the stack of Middleware objects, re-raising an
  16. error quickly pollutes the traceback displayed.
  17. Runserver_plus only needs needs traceback frames relevant to WSGIHandler Middleware objects, so
  18. only store the traceback if it is for a WSGIHandler. If an exception is not raised here, Django
  19. eventually throws an error for not getting a valid response object for its debug view.
  20. """
  21. try:
  22. # Store the most recent tb for WSGI requests. The class can be found in the second frame of the tb
  23. if isinstance(tb.tb_next.tb_frame.f_locals.get('self'), WSGIHandler):
  24. tld.wsgi_tb = tb
  25. elif tld.wsgi_tb:
  26. tb = tld.wsgi_tb
  27. except AttributeError:
  28. pass
  29. six.reraise(exc_type, exc_value, tb)