Include connection errors in exception handling

This commit is contained in:
2022-08-20 12:58:47 +01:00
parent 168b2b288a
commit 72f2af1df8
15 changed files with 63 additions and 68 deletions

View File

@@ -58,7 +58,7 @@ class User(UserMixin, db.Model):
def register(self, notify:bool=False, password:str=None):
self.generate_id()
try: users = User.query.all()
except SQLAlchemyError as exception:
except (SQLAlchemyError, ConnectionError) as exception:
write('system.log', f'Database error when setting default dataset {self.id}: {exception}')
return False, f'Database error {exception}.'
for user in users:
@@ -68,7 +68,7 @@ class User(UserMixin, db.Model):
try:
db.session.add(self)
db.session.commit()
except SQLAlchemyError as exception:
except (SQLAlchemyError, ConnectionError) as exception:
db.session.rollback()
write('system.log', f'Database error when registering user {self.get_username()}: {exception}')
return False, f'Database error: {exception}'
@@ -101,8 +101,7 @@ class User(UserMixin, db.Model):
"""
)
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}')
except (SMTPException, ConnectionError) 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):
@@ -154,12 +153,12 @@ class User(UserMixin, db.Model):
"""
)
try: mail.send(email)
except SMTPException as exception:
except (SMTPException, ConnectionError) as exception:
write('system.log', f'SMTP Error while trying to reset password for {self.get_username()} with error: {exception}')
db.session.rollback()
return jsonify({'error': f'SMTP Error: {exception}'}), 500
try: db.session.commit()
except SQLAlchemyError as exception:
except (SQLAlchemyError, ConnectionError) as exception:
db.session.rollback()
write('system.log', f'Database error when resetting password for user {self.get_username()}: {exception}')
return False, f'Database error: {exception}'
@@ -168,7 +167,7 @@ class User(UserMixin, db.Model):
def clear_reset_tokens(self):
self.reset_token = self.verification_token = None
try: db.session.commit()
except SQLAlchemyError as exception:
except (SQLAlchemyError, ConnectionError) as exception:
db.session.rollback()
write('system.log', f'Database error when resetting clearing reset tokens for user {self.get_username()}: {exception}')
return False, f'Database error: {exception}'
@@ -179,7 +178,7 @@ class User(UserMixin, db.Model):
try:
db.session.delete(self)
db.session.commit()
except SQLAlchemyError as exception:
except (SQLAlchemyError, ConnectionError) as exception:
db.session.rollback()
write('system.log', f'Database error when deleting user {self.get_username()}: {exception}')
return False, f'Database error: {exception}'
@@ -208,8 +207,7 @@ class User(UserMixin, db.Model):
"""
)
try: mail.send(email)
except SMTPException as exception:
write('system.log', f'SMTP Error when trying to delete account {username} with error: {exception}')
except (SMTPException, ConnectionError) 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):
@@ -220,12 +218,12 @@ class User(UserMixin, db.Model):
try:
for entry in User.query.all():
if entry.get_email() == email and not entry == self: return False, f'The email address {email} is already in use.'
except SQLAlchemyError as exception:
except (SQLAlchemyError, ConnectionError) as exception:
write('system.log', f'Database error when setting default dataset {self.id}: {exception}')
return False, f'Database error {exception}.'
self.set_email(email)
try: db.session.commit()
except SQLAlchemyError as exception:
except (SQLAlchemyError, ConnectionError) as exception:
db.session.rollback()
write('system.log', f'Database error when updating user {self.get_username()}: {exception}')
return False, f'Database error: {exception}'
@@ -259,6 +257,5 @@ class User(UserMixin, db.Model):
"""
)
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}')
except (SMTPException, ConnectionError) 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.'