简体   繁体   中英

Django: Send mail to admin on every error raised

I have a project where we process data from various sources. My problem is I want a to send email every time there is an error or error raise my me.

Currently I do something like this where on every raise I send a email. But I want a system where on every error there would be a email sent.


# send an email alert
email_content = "Email content"
subject = "Subject"
if settings.ENVIRONMENT == 'PROD':
    BaseScraper.email_alert_for_error(
       subject,
       email_content,
       ["admin@mail.com"],
       )
else:
    BaseScraper.email_alert_for_error(
       subject,
       email_content,
       ["admin@mail.com"],
       )

You can use try: and except: like such:

 try:
     <do something>
 except Exception as e:
      email_content = email_content + e
      BaseScraper.email_alert_for_error(
           subject,
           email_content,
           ["admin@mail.com"],
      )

Here a list of keyword for exception: https://docs.djangoproject.com/en/4.1/ref/exceptions/

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