exceptionhandler.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. from rest_framework.views import exception_handler as rest_exception_handler
  2. from django.core.exceptions import PermissionDenied
  3. from django.http import Http404, HttpResponsePermanentRedirect, JsonResponse
  4. from django.urls import reverse
  5. from social_core.exceptions import SocialAuthBaseException
  6. from social_core.utils import social_logger
  7. from . import errorpages
  8. from .exceptions import AjaxError, Banned, ExplicitFirstPage, OutdatedSlug
  9. HANDLED_EXCEPTIONS = (
  10. AjaxError,
  11. Banned,
  12. ExplicitFirstPage,
  13. Http404,
  14. OutdatedSlug,
  15. PermissionDenied,
  16. SocialAuthBaseException,
  17. )
  18. def is_misago_exception(exception):
  19. return isinstance(exception, HANDLED_EXCEPTIONS)
  20. def handle_ajax_error(request, exception):
  21. json = {
  22. 'is_error': 1,
  23. 'message': str(exception.message),
  24. }
  25. return JsonResponse(json, status=exception.code)
  26. def handle_banned_exception(request, exception):
  27. return errorpages.banned(request, exception)
  28. def handle_explicit_first_page_exception(request, exception):
  29. matched_url = request.resolver_match.url_name
  30. if request.resolver_match.namespace:
  31. matched_url = '%s:%s' % (request.resolver_match.namespace, matched_url)
  32. url_kwargs = request.resolver_match.kwargs
  33. del url_kwargs['page']
  34. new_url = reverse(matched_url, kwargs=url_kwargs)
  35. return HttpResponsePermanentRedirect(new_url)
  36. def handle_http404_exception(request, exception):
  37. return errorpages.page_not_found(request, exception)
  38. def handle_outdated_slug_exception(request, exception):
  39. view_name = request.resolver_match.view_name
  40. model = exception.args[0]
  41. url_kwargs = request.resolver_match.kwargs
  42. url_kwargs['slug'] = model.slug
  43. new_url = reverse(view_name, kwargs=url_kwargs)
  44. return HttpResponsePermanentRedirect(new_url)
  45. def handle_permission_denied_exception(request, exception):
  46. return errorpages.permission_denied(request, exception)
  47. def handle_social_auth_exception(request, exception):
  48. social_logger.error(exception)
  49. return errorpages.social_auth_failed(request, exception)
  50. EXCEPTION_HANDLERS = [
  51. (AjaxError, handle_ajax_error),
  52. (Banned, handle_banned_exception),
  53. (Http404, handle_http404_exception),
  54. (ExplicitFirstPage, handle_explicit_first_page_exception),
  55. (OutdatedSlug, handle_outdated_slug_exception),
  56. (PermissionDenied, handle_permission_denied_exception),
  57. (SocialAuthBaseException, handle_social_auth_exception),
  58. ]
  59. def get_exception_handler(exception):
  60. for exception_type, handler in EXCEPTION_HANDLERS:
  61. if isinstance(exception, exception_type):
  62. return handler
  63. else:
  64. raise ValueError("%s is not Misago exception" % exception.__class__.__name__)
  65. def handle_misago_exception(request, exception):
  66. handler = get_exception_handler(exception)
  67. return handler(request, exception)
  68. def handle_api_exception(exception, context):
  69. response = rest_exception_handler(exception, context)
  70. if response:
  71. if isinstance(exception, Banned):
  72. response.data['ban'] = exception.ban.get_serialized_message()
  73. elif isinstance(exception, PermissionDenied):
  74. try:
  75. response.data['detail'] = exception.args[0]
  76. except IndexError:
  77. pass
  78. return response
  79. else:
  80. return None