简体   繁体   English

如何使用 Flask 为 500 错误设置 email 警报?

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

I am using flask, marshmallow, and sql alchemy to make an API.我正在使用 flask、棉花糖和 sql 炼金术来制作 API。 I want to configure email alerts on 500 errors.我想在 500 错误时配置 email 警报。 The other error types I have the email alerts for work fine.其他错误类型我有 email 警报可以正常工作。

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: Function 在 500 错误处理程序中调用:

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.我怀疑e == 500条件总是评估为False即使在 500 错误的情况下。

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) ?如果你用@app.errorhandler(500)装饰处理程序,这个测试的目的是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用conda为Flask设置虚拟环境? - How do I set up a virtual environment with Flask using conda? 如何设置 virtualenv 并用它设置烧瓶? - How do I set up a virtualenv and set up flask with it? 如何在AWS实例上设置警报以在数据分析完成时通知我? - How do I set up alerts on AWS instances to let me know when my data analysis is complete? 如何使用应用程序工厂正确设置flask-admin视图? - How do I properly set up flask-admin views with using an application factory? 如何使用 flask 应用程序的文本和图像设置 html? - how do I set up html with text and images for flask app? 如何使用MySQL作为数据库在金字塔中设置登录系统以存储电子邮件和密码? - How do I set up a login system in pyramid using mysql as database to store email and password? 如何使用Flask打开下载对话框? - How do I open up a download dialog box using Flask? 如何设置 Flask 请求以更改 HTML 按钮的颜色? - How do I set up a Flask request to change the color of an HTML button? 如何在 Flask 中设置响应头? - How do I set response headers in Flask? 如何解决使用flask-mail发送电子邮件时的buildins.ConnectionRefusedError错误 - How do I resolve builtins.ConnectionRefusedError error in attempting to send email using flask-mail
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM