exceptionhandler.py 3.3 KB

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