You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

exception.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import logging
  2. import sys
  3. from functools import wraps
  4. from django.conf import settings
  5. from django.core import signals
  6. from django.core.exceptions import (
  7. PermissionDenied, RequestDataTooBig, SuspiciousOperation,
  8. TooManyFieldsSent,
  9. )
  10. from django.http import Http404
  11. from django.http.multipartparser import MultiPartParserError
  12. from django.urls import get_resolver, get_urlconf
  13. from django.utils.log import log_response
  14. from django.views import debug
  15. def convert_exception_to_response(get_response):
  16. """
  17. Wrap the given get_response callable in exception-to-response conversion.
  18. All exceptions will be converted. All known 4xx exceptions (Http404,
  19. PermissionDenied, MultiPartParserError, SuspiciousOperation) will be
  20. converted to the appropriate response, and all other exceptions will be
  21. converted to 500 responses.
  22. This decorator is automatically applied to all middleware to ensure that
  23. no middleware leaks an exception and that the next middleware in the stack
  24. can rely on getting a response instead of an exception.
  25. """
  26. @wraps(get_response)
  27. def inner(request):
  28. try:
  29. response = get_response(request)
  30. except Exception as exc:
  31. response = response_for_exception(request, exc)
  32. return response
  33. return inner
  34. def response_for_exception(request, exc):
  35. if isinstance(exc, Http404):
  36. if settings.DEBUG:
  37. response = debug.technical_404_response(request, exc)
  38. else:
  39. response = get_exception_response(request, get_resolver(get_urlconf()), 404, exc)
  40. elif isinstance(exc, PermissionDenied):
  41. response = get_exception_response(request, get_resolver(get_urlconf()), 403, exc)
  42. log_response(
  43. 'Forbidden (Permission denied): %s', request.path,
  44. response=response,
  45. request=request,
  46. exc_info=sys.exc_info(),
  47. )
  48. elif isinstance(exc, MultiPartParserError):
  49. response = get_exception_response(request, get_resolver(get_urlconf()), 400, exc)
  50. log_response(
  51. 'Bad request (Unable to parse request body): %s', request.path,
  52. response=response,
  53. request=request,
  54. exc_info=sys.exc_info(),
  55. )
  56. elif isinstance(exc, SuspiciousOperation):
  57. if isinstance(exc, (RequestDataTooBig, TooManyFieldsSent)):
  58. # POST data can't be accessed again, otherwise the original
  59. # exception would be raised.
  60. request._mark_post_parse_error()
  61. # The request logger receives events for any problematic request
  62. # The security logger receives events for all SuspiciousOperations
  63. security_logger = logging.getLogger('django.security.%s' % exc.__class__.__name__)
  64. security_logger.error(
  65. str(exc),
  66. extra={'status_code': 400, 'request': request},
  67. )
  68. if settings.DEBUG:
  69. response = debug.technical_500_response(request, *sys.exc_info(), status_code=400)
  70. else:
  71. response = get_exception_response(request, get_resolver(get_urlconf()), 400, exc)
  72. elif isinstance(exc, SystemExit):
  73. # Allow sys.exit() to actually exit. See tickets #1023 and #4701
  74. raise
  75. else:
  76. signals.got_request_exception.send(sender=None, request=request)
  77. response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
  78. log_response(
  79. '%s: %s', response.reason_phrase, request.path,
  80. response=response,
  81. request=request,
  82. exc_info=sys.exc_info(),
  83. )
  84. # Force a TemplateResponse to be rendered.
  85. if not getattr(response, 'is_rendered', True) and callable(getattr(response, 'render', None)):
  86. response = response.render()
  87. return response
  88. def get_exception_response(request, resolver, status_code, exception):
  89. try:
  90. callback, param_dict = resolver.resolve_error_handler(status_code)
  91. response = callback(request, **{**param_dict, 'exception': exception})
  92. except Exception:
  93. signals.got_request_exception.send(sender=None, request=request)
  94. response = handle_uncaught_exception(request, resolver, sys.exc_info())
  95. return response
  96. def handle_uncaught_exception(request, resolver, exc_info):
  97. """
  98. Processing for any otherwise uncaught exceptions (those that will
  99. generate HTTP 500 responses).
  100. """
  101. if settings.DEBUG_PROPAGATE_EXCEPTIONS:
  102. raise
  103. if settings.DEBUG:
  104. return debug.technical_500_response(request, *exc_info)
  105. # Return an HttpResponse that displays a friendly error message.
  106. callback, param_dict = resolver.resolve_error_handler(500)
  107. return callback(request, **param_dict)