示例#1
0
文件: api.py 项目: sshyran/zing
    def handle_exception(self, exc):
        """Handles response exceptions."""
        if isinstance(exc, Http404):
            return JsonResponseNotFound({"msg": "Not found"})

        if isinstance(exc, PermissionDenied):
            return JsonResponseForbidden({"msg": "Permission denied."})

        if isinstance(exc, JSONDecodeError):
            return JsonResponseBadRequest({"msg": "Invalid JSON data"})

        raise
示例#2
0
    def process_exception(self, request, exception):
        msg = force_str(exception)
        if isinstance(exception, Http404):
            if request.is_ajax():
                return JsonResponseNotFound({"msg": msg})
        elif isinstance(exception, Http400):
            if request.is_ajax():
                return JsonResponseBadRequest({"msg": msg})
        elif isinstance(exception, PermissionDenied):
            if request.is_ajax():
                return JsonResponseForbidden({"msg": msg})

            ctx = {
                "permission_error": msg,
            }

            if not request.user.is_authenticated:
                msg_args = {
                    "login_link": reverse("account_login"),
                }
                login_msg = _(
                    'You need to <a class="js-login" '
                    'href="%(login_link)s">login</a> to access this page.' %
                    msg_args)
                ctx["login_message"] = login_msg

            return HttpResponseForbidden(
                render_to_string("errors/403.html",
                                 context=ctx,
                                 request=request))
        elif exception.__class__.__name__ in (
                "OperationalError",
                "ProgrammingError",
                "DatabaseError",
        ):
            # HACKISH: Since exceptions thrown by different databases do not
            # share the same class heirarchy (DBAPI2 sucks) we have to check
            # the class name instead. Since python uses duck typing I will call
            # this poking-the-duck-until-it-quacks-like-a-duck-test
            return handle_exception(request, exception, "errors/db.html")
        else:
            return handle_exception(request, exception, "errors/500.html")
示例#3
0
    def process_exception(self, request, exception):
        msg = force_unicode(exception)
        if isinstance(exception, Http404):
            if request.is_ajax():
                return JsonResponseNotFound({'msg': msg})
        elif isinstance(exception, Http400):
            if request.is_ajax():
                return JsonResponseBadRequest({'msg': msg})
        elif isinstance(exception, PermissionDenied):
            if request.is_ajax():
                return JsonResponseForbidden({'msg': msg})

            ctx = {
                'permission_error': msg,
            }

            if not request.user.is_authenticated():
                msg_args = {
                    'login_link': reverse('account_login'),
                }
                login_msg = _(
                    'You need to <a class="js-login" href="%(login_link)s">login</a> '
                    'to access this page.', msg_args)
                ctx["login_message"] = login_msg

            return HttpResponseForbidden(
                render_to_string('errors/403.html', ctx,
                                 RequestContext(request)))
        elif (exception.__class__.__name__
              in ('OperationalError', 'ProgrammingError', 'DatabaseError')):
            # HACKISH: Since exceptions thrown by different databases do
            # not share the same class heirarchy (DBAPI2 sucks) we have to
            # check the class name instead. Since python uses duck typing
            # I will call this
            # poking-the-duck-until-it-quacks-like-a-duck-test
            return handle_exception(request, exception, 'errors/db.html')
        else:
            return handle_exception(request, exception, 'errors/500.html')