Rafał Pitoń 11 лет назад
Родитель
Сommit
cec09a6db6
2 измененных файлов с 17 добавлено и 4 удалено
  1. 10 4
      misago/core/exceptionhandler.py
  2. 7 0
      misago/core/exceptions.py

+ 10 - 4
misago/core/exceptionhandler.py

@@ -1,19 +1,24 @@
 from django.core.exceptions import PermissionDenied
 from django.core.exceptions import PermissionDenied
 from django.core.urlresolvers import reverse
 from django.core.urlresolvers import reverse
-from django.http import Http404, HttpResponsePermanentRedirect
+from django.http import Http404, HttpResponsePermanentRedirect, JsonResponse
 
 
 from misago.core import errorpages
 from misago.core import errorpages
-from misago.core.exceptions import ExplicitFirstPage, OutdatedSlug
+from misago.core.exceptions import AjaxError, ExplicitFirstPage, OutdatedSlug
 
 
 
 
-HANDLED_EXCEPTIONS = (ExplicitFirstPage, Http404,
-                      OutdatedSlug, PermissionDenied,)
+HANDLED_EXCEPTIONS = (AjaxError, ExplicitFirstPage, Http404,
+                      OutdatedSlug, PermissionDenied)
 
 
 
 
 def is_misago_exception(exception):
 def is_misago_exception(exception):
     return exception.__class__ in HANDLED_EXCEPTIONS
     return exception.__class__ in HANDLED_EXCEPTIONS
 
 
 
 
+def handle_ajax_error(request, exception):
+    json = {'is_error': 1, 'message': exception.message}
+    return JsonResponse(json, code=exception.code)
+
+
 def handle_explicit_first_page_exception(request, exception):
 def handle_explicit_first_page_exception(request, exception):
     matched_url = request.resolver_match.url_name
     matched_url = request.resolver_match.url_name
     if request.resolver_match.namespace:
     if request.resolver_match.namespace:
@@ -54,6 +59,7 @@ def handle_permission_denied_exception(request, exception):
 
 
 
 
 EXCEPTION_HANDLERS = (
 EXCEPTION_HANDLERS = (
+    (AjaxError, handle_ajax_error),
     (Http404, handle_http404_exception),
     (Http404, handle_http404_exception),
     (ExplicitFirstPage, handle_explicit_first_page_exception),
     (ExplicitFirstPage, handle_explicit_first_page_exception),
     (OutdatedSlug, handle_outdated_slug_exception),
     (OutdatedSlug, handle_outdated_slug_exception),

+ 7 - 0
misago/core/exceptions.py

@@ -1,3 +1,10 @@
+class AjaxError(Exception):
+    """You've tried to do something over AJAX but misago blurped"""
+    def __init__(self, message=None, code=406):
+        self.message = message
+        self.code = code
+
+
 class ExplicitFirstPage(Exception):
 class ExplicitFirstPage(Exception):
     """The url that was used to reach view contained explicit first page"""
     """The url that was used to reach view contained explicit first page"""
     pass
     pass