如何在 python 中发生 500 错误时向管理员发送电子邮件。
我使用的网络框架是“bottle”。
最佳答案
只需使用 @error(code)
装饰器来定义错误处理页面,如下所示:
from bottle import run, error, route
@error(500)
def handle_500_error(code):
# add mail send code here
return "Error message here"
@route("/test_500")
def cause_error():
raise Exception
run()
只需导航至 /test_500
即可查看其运行情况
您当然可以像使用任何其他页面一样为错误页面使用模板。我不确定在有错误处理程序的同时是否有办法获取内置的 Bottle 错误页面。
编辑:
显然,如果您使用的是最新的 Bottle v0.8,您应用 @error
装饰器的函数接收到的参数不是错误代码,而是 bottle.HTTPError
。对象,其中包含异常和回溯。
或者,您可以通过将 bottle.app().catchall
设置为 False
来将 Bottle 设置为不处理异常,如 here 所述,然后使用一些适当的 WSGI 中间件来处理它们并发送电子邮件(例如 this 之类的东西)。
关于python - 发生 500 错误时向管理员发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3324743/