Generalise exception handling
This commit is contained in:
@@ -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, ConnectionError) as exception:
|
||||
except Exception 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, ConnectionError) as exception:
|
||||
except Exception 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,7 +101,7 @@ class User(UserMixin, db.Model):
|
||||
"""
|
||||
)
|
||||
try: mail.send(email)
|
||||
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}')
|
||||
except Exception 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):
|
||||
@@ -153,12 +153,12 @@ class User(UserMixin, db.Model):
|
||||
"""
|
||||
)
|
||||
try: mail.send(email)
|
||||
except (SMTPException, ConnectionError) as exception:
|
||||
except Exception 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, ConnectionError) as exception:
|
||||
except Exception 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}'
|
||||
@@ -167,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, ConnectionError) as exception:
|
||||
except Exception 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}'
|
||||
@@ -178,7 +178,7 @@ class User(UserMixin, db.Model):
|
||||
try:
|
||||
db.session.delete(self)
|
||||
db.session.commit()
|
||||
except (SQLAlchemyError, ConnectionError) as exception:
|
||||
except Exception as exception:
|
||||
db.session.rollback()
|
||||
write('system.log', f'Database error when deleting user {self.get_username()}: {exception}')
|
||||
return False, f'Database error: {exception}'
|
||||
@@ -207,7 +207,7 @@ class User(UserMixin, db.Model):
|
||||
"""
|
||||
)
|
||||
try: mail.send(email)
|
||||
except (SMTPException, ConnectionError) as exception: write('system.log', f'SMTP Error when trying to delete account {username} with error: {exception}')
|
||||
except Exception 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):
|
||||
@@ -218,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, ConnectionError) as exception:
|
||||
except Exception 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, ConnectionError) as exception:
|
||||
except Exception as exception:
|
||||
db.session.rollback()
|
||||
write('system.log', f'Database error when updating user {self.get_username()}: {exception}')
|
||||
return False, f'Database error: {exception}'
|
||||
@@ -257,5 +257,5 @@ class User(UserMixin, db.Model):
|
||||
"""
|
||||
)
|
||||
try: mail.send(message)
|
||||
except (SMTPException, ConnectionError) as exception: write('system.log', f'SMTP Error when trying to update account {self.get_username()} with error: {exception}')
|
||||
except Exception 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.'
|
||||
|
Reference in New Issue
Block a user