exceptionhandler.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from django.core.exceptions import PermissionDenied
  2. from django.http import Http404
  3. from misago.views import errorpages
  4. from misago.views.exceptions import OutdatedSlug
  5. HANDLED_EXCEPTIONS = (Http404, OutdatedSlug, PermissionDenied,)
  6. def is_misago_exception(exception):
  7. return exception.__class__ in HANDLED_EXCEPTIONS
  8. def _get_exception_message(exception):
  9. try:
  10. return exception.args[0]
  11. except IndexError:
  12. return None
  13. def handle_http404_exception(request, exception):
  14. return errorpages.page_not_found(request)
  15. def handle_outdated_slug_exception(request, exception):
  16. raise NotImplementedError()
  17. def handle_permission_denied_exception(request, exception):
  18. return errorpages.permission_denied(request,
  19. _get_exception_message(exception))
  20. EXCEPTION_HANDLERS = (
  21. (Http404, handle_http404_exception),
  22. (OutdatedSlug, handle_outdated_slug_exception),
  23. (PermissionDenied, handle_permission_denied_exception),
  24. )
  25. def get_exception_handler(exception):
  26. for exception_type, handler in EXCEPTION_HANDLERS:
  27. if isinstance(exception, exception_type):
  28. return handler
  29. else:
  30. raise ValueError(
  31. "%s is not Misago exception" % exception.__class__.__name__)
  32. def handle_misago_exception(request, exception):
  33. handler = get_exception_handler(exception)
  34. return handler(request, exception)