Created
January 22, 2024 15:11
-
-
Save tschreiner/dd9f290ebc8fc65f96dffc1d84fa59e7 to your computer and use it in GitHub Desktop.
Exception handler for sending unhandled exceptions to the developer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import traceback | |
| import smtplib | |
| from email.mime.text import MIMEText | |
| def send_error_to_developer(error_details): | |
| msg = MIMEText(error_details) | |
| msg['Subject'] = 'Application Error Report' | |
| msg['From'] = '[email protected]' | |
| msg['To'] = '[email protected]' | |
| # Send the message via SMTP server | |
| s = smtplib.SMTP('localhost') | |
| s.send_message(msg) | |
| s.quit() | |
| def global_exception_handler(exctype, value, tb): | |
| error_details = ''.join(traceback.format_exception(exctype, value, tb)) | |
| send_error_to_developer(error_details) | |
| # Optionally, inform the user that an error occurred | |
| import sys | |
| sys.excepthook = global_exception_handler | |
| # Your application code here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment