简体   繁体   中英

How do I set up email alerts for 500 errors using Flask?

I am using flask, marshmallow, and sql alchemy to make an API. I want to configure email alerts on 500 errors. The other error types I have the email alerts for work fine.

Error Handling Code

@app.errorhandler(ValidationError)
def handle_marshmallow_validation(err):  # except ValidationError as err
    return jsonify(err.messages), 400

@app.errorhandler(500)
def server_error(e):
    if e == 500:
        error_500_email()

Function called in the 500 error handler:

def error_500_email():
    s = smtplib.SMTP(host='mailo2.uhc.com', port=25)
    text = "There was an error"
    msg = MIMEText(str(text))
    msg['Subject'] = 'Prod SA Tool Error'
    s.sendmail('sa_prod@optum.com', 'ian.christ@optum.com', msg.as_string())
    s.quit()

I suspect the e == 500 condition always evaluates to False even in case of 500 error.

You could fix it, but why not remove it? What's the purpose of this test if you decorate the handler with @app.errorhandler(500) ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM