Emailing admin when a 500 error occurs

佐手、 提交于 2019-12-23 12:49:09

问题


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

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


回答1:


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()

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.

Edit:

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.

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).




回答2:


The following is a line from the Bottle documentation.

All unhandled exceptions other than bottle.HTTPError will result in a 500 Internal Server Error response, so they won't crash your WSGI server.

Judging by this you would want to catch those Exceptions and write the code to send an email to whomsoever it may concern. Your code will go into the try block and you will have a some code for the bottle.HTTPError exception and then code to catch all other Exceptions which sends the desired email.



来源:https://stackoverflow.com/questions/3324743/emailing-admin-when-a-500-error-occurs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!