在自定义500处理程序中捕获错误文本 [英] Catch error text in custom 500 handler
本文介绍了在自定义500处理程序中捕获错误文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的某个地方,我抛出了一个错误,尤其是这个错误:
Somewhere in my views, I throw an error, particularly this one:
views.py
from xmlrpclib import Fault
def some_function(request):
if ....:
return Fault(-1, 'foo')
然后,也在 views.py 中,我有自定义的500处理程序来捕获服务器错误:
Then, also in views.py, I have my custom 500 handler to catch server errors:
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)
无论如何我都可以访问该错误消息?谢谢
Is there anyway in which I can access the error message? Thanks
推荐答案
尝试覆盖 urls.py
中的 django.conf.urls.defaults.handler500
.
from django.conf.urls.defaults import *
handler500 = 'path.to.my_custom_500'
甚至更好-编写自己的处理程序并将其放在 LOGGING
设置.
or even better - write your own handler and put it in the LOGGING
settings.
修改:
您还可以在您的 my_custom_500
代码中添加可识别异常类型的代码,例如:
You can also add to your my_custom_500
code that will recognize type of exception, eg:
import sys;
def my_custom_500(request):
...
type_, value, traceback = sys.exc_info()
...
这篇关于在自定义500处理程序中捕获错误文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文