发生500错误时通过电子邮件发送给管理员 [英] Emailing admin when a 500 error occurs

查看:245
本文介绍了发生500错误时通过电子邮件发送给管理员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当发生500错误时,如何在python中发送电子邮件给管理员。

How can I send an email to admin when a 500 error occurs, in python.

我正在使用的Web框架是瓶子。

The web framework I'm using is 'bottle'.

推荐答案

只需使用 @error(code)装饰器来定义错误处理页面,就像这样:

Just use the @error(code) decorator to define an error handling page, like so:

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 即可查看运行情况

Just navigate to /test_500 to see it in action

您当然可以在错误页面上使用模板像其他页面一样我不确定是否有办法在拥有错误处理程序的同时获取内置瓶错误页面。

You can of course use a template for the error page just like with any other page. I'm not sure if there's a way to get the built-in bottle error page while having an error handler.

编辑:

显然,如果您使用的是最新的Bottle v0.8,则将 @error 装饰器应用到的函数将作为参数接收,而不是错误代码,但 bottle.HTTPError 对象,其中包含异常和追溯。

Apparently if you're using the latest Bottle v0.8, the function to which you apply the @error decorator receives as a parameter not the error code, but an bottle.HTTPError object, which contains the exception and traceback.

或者,您可以通过设置 bottle将Bottle设置为不处理异常。 app()。catchall False ,如此处,然后使用一些适当的WSGI中间件来处理它们并发送电子邮件(例如,诸如)。

Alternatively, you can set Bottle to not handle exceptions by setting bottle.app().catchall to False as described here, and then use some appropriate WSGI middleware to handle them and send the email (e.g. something like this).

这篇关于发生500错误时通过电子邮件发送给管理员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆