errorpages.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:zing 作者: evernote 项目源码 文件源码
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', 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')
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号