exceptionhandler.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. _get_exception_message(exception))
  16. def handle_outdated_slug_exception(request, exception):
  17. raise NotImplementedError()
  18. def handle_permission_denied_exception(request, exception):
  19. return errorpages.permission_denied(request,
  20. _get_exception_message(exception))
  21. EXCEPTION_HANDLERS = (
  22. (Http404, handle_http404_exception),
  23. (OutdatedSlug, handle_outdated_slug_exception),
  24. (PermissionDenied, handle_permission_denied_exception),
  25. )
  26. def get_exception_handler(exception):
  27. for exception_type, handler in EXCEPTION_HANDLERS:
  28. if isinstance(exception, exception_type):
  29. return handler
  30. else:
  31. raise ValueError(
  32. "%s is not Misago exception" % exception.__class__.__name__)
  33. def handle_misago_exception(request, exception):
  34. handler = get_exception_handler(exception)
  35. return handler(request, exception)