From 1c579505586e9ced3a0a2d2794b7729779f9404d Mon Sep 17 00:00:00 2001 From: Vivek Santayana Date: Fri, 19 Aug 2022 12:07:38 +0100 Subject: [PATCH] Exception handling and logging for SMTP errors Should mitigate internal server errors if SMTP server fails. --- ref-test/app/models/entry.py | 6 +++++- ref-test/app/models/user.py | 28 +++++++++++++++++++--------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/ref-test/app/models/entry.py b/ref-test/app/models/entry.py index 4e528e4..b215afc 100644 --- a/ref-test/app/models/entry.py +++ b/ref-test/app/models/entry.py @@ -6,6 +6,7 @@ from .test import Test from flask_login import current_user from flask_mail import Message +from smtplib import SMTPException from datetime import datetime, timedelta from uuid import uuid4 @@ -174,4 +175,7 @@ class Entry(db.Model):

Best wishes,
SKA Refereeing

""" ) - mail.send(email) \ No newline at end of file + try: + mail.send(email) + except SMTPException as exception: + write('system.log', f'SMTP Error when trying to notify results to {self.get_surname()}, {self.get_first_name()} with error: {exception}') \ No newline at end of file diff --git a/ref-test/app/models/user.py b/ref-test/app/models/user.py index c2a5d81..c7e262d 100644 --- a/ref-test/app/models/user.py +++ b/ref-test/app/models/user.py @@ -6,6 +6,7 @@ from flask import flash, jsonify, session from flask.helpers import url_for from flask_login import current_user, login_user, logout_user, UserMixin from flask_mail import Message +from smtplib import SMTPException from werkzeug.security import check_password_hash, generate_password_hash import secrets @@ -90,7 +91,10 @@ class User(UserMixin, db.Model):

SKA Refereeing

""" ) - mail.send(email) + try: + mail.send(email) + except SMTPException as exception: + write('system.log', f'SMTP Error while trying to notify new user account creation to {self.get_username()} with error: {exception}') return True, f'User {self.get_username()} was created successfully.' def login(self, remember:bool=False): @@ -109,7 +113,6 @@ class User(UserMixin, db.Model): self.set_password(new_password) self.reset_token = secrets.token_urlsafe(16) self.verification_token = secrets.token_urlsafe(16) - db.session.commit() email = Message( subject='RefTest | Password Reset', recipients=[self.get_email()], @@ -142,11 +145,12 @@ class User(UserMixin, db.Model):

SKA Refereeing

""" ) - mail.send(email) - print('Password', new_password) - print('Reset Token', self.reset_token) - print('Verification Token', self.verification_token) - print('Reset Link', f'{url_for("admin._reset", token=self.reset_token, verification=self.verification_token, _external=True)}') + try: + mail.send(email) + except SMTPException as exception: + write('system.log', f'SMTP Error while trying to reset password for {self.get_username()} with error: {exception}') + return jsonify({'error': f'SMTP Error: {exception}'}), 500 + db.session.commit() return jsonify({'success': 'Your password reset link has been generated.'}), 200 def clear_reset_tokens(self): @@ -182,7 +186,10 @@ class User(UserMixin, db.Model):

SKA Refereeing

""" ) - mail.send(email) + try: + mail.send(email) + except SMTPException as exception: + write('system.log', f'SMTP Error when trying to delete account {username} with error: {exception}') return True, message def update(self, password:str=None, email:str=None, notify:bool=False): @@ -223,5 +230,8 @@ class User(UserMixin, db.Model):

SKA Refereeing

""" ) - mail.send(message) + try: + mail.send(message) + except SMTPException as exception: + write('system.log', f'SMTP Error when trying to update account {self.get_username()} with error: {exception}') return True, f'Account {self.get_username()} has been updated.'