【问题标题】:Catch error text in custom 500 handler在自定义 500 处理程序中捕获错误文本
【发布时间】:2014-07-01 13:07:05
【问题描述】:

在我看来,我抛出了一个错误,尤其是这个:

views.py

from xmlrpclib import Fault

def some_function(request):
    if ....:
        return Fault(-1, 'foo')

然后,同样在 views.py 中,我有我的自定义 500 处理程序来捕获服务器错误:

def my_custom_500(request):
    context = {...}
    ### Here is where I need to catch `'foo'` 
    ### in order to put it in the context and pass it to the template
    render(request, '500.html', context)

无论如何我可以访问错误消息吗? 谢谢

【问题讨论】:

    标签: django


    【解决方案1】:

    尝试在您的urls.py 中覆盖django.conf.urls.defaults.handler500

    from django.conf.urls.defaults import *
    handler500 = 'path.to.my_custom_500'
    

    甚至更好 - 编写您自己的处理程序并将其放入 LOGGING settings

    编辑

    您还可以将识别异常类型的代码添加到您的my_custom_500,例如:

    import sys; 
    
    def my_custom_500(request):
        ...
        type_, value, traceback = sys.exc_info()
        ...
    

    【讨论】:

    • 这不是我的问题。我已经做了这个覆盖。我需要的是访问触发自定义 500 的错误消息。
    • 添加到您的my_custom_500 函数:import sys; type, value, traceback = sys.exc_info() 并检查您的will get
    • 是的,我刚刚在某处找到了完全相同的解决方案......将其发布为答案,以便我投票
    猜你喜欢
    • 1970-01-01
    • 2011-07-27
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    • 2014-03-07
    • 2018-04-29
    • 2011-06-01
    • 1970-01-01
    相关资源
    最近更新 更多