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

@ -31,7 +31,7 @@ def _home():
try:
tests = Test.query.all()
results = Entry.query.all()
except SQLAlchemyError as exception:
except (SQLAlchemyError, ConnectionError) as exception:
write('system.log', f'Database error when processing request \'{request.url}\': {exception}')
return abort(500)
current_tests = [ test for test in tests if test.end_date >= datetime.now() and test.start_date.date() <= date.today() ]
@ -48,7 +48,7 @@ def _settings():
try:
users = User.query.all()
datasets = Dataset.query.all()
except SQLAlchemyError as exception:
except (SQLAlchemyError, ConnectionError) as exception:
write('system.log', f'Database error when processing request \'{request.url}\': {exception}')
return abort(500)
return render_template('/admin/settings/index.html', users=users, datasets=datasets)
@ -61,7 +61,7 @@ def _login():
if request.method == 'POST':
if form.validate_on_submit():
try: users = User.query.all()
except SQLAlchemyError as exception:
except (SQLAlchemyError, ConnectionError) as exception:
write('system.log', f'Database error when processing request \'{request.url}\': {exception}')
return abort(500)
user = None
@ -113,7 +113,7 @@ def _reset():
if form.validate_on_submit():
user = None
try: users = User.query.all()
except SQLAlchemyError as exception:
except (SQLAlchemyError, ConnectionError) as exception:
write('system.log', f'Database error when processing request \'{request.url}\': {exception}')
return abort(500)
for _user in users:
@ -128,7 +128,7 @@ def _reset():
token = request.args.get('token')
if token:
try: user = User.query.filter_by(reset_token=token).first()
except SQLAlchemyError as exception:
except (SQLAlchemyError, ConnectionError) as exception:
write('system.log', f'Database error when processing request \'{request.url}\': {exception}')
return abort(500)
if not user: return redirect(url_for('admin._reset'))
@ -148,7 +148,7 @@ def _update_password():
if form.validate_on_submit():
user = session.pop('user')
try: user = User.query.filter_by(id=user).first()
except SQLAlchemyError as exception:
except (SQLAlchemyError, ConnectionError) as exception:
write('system.log', f'Database error when processing request \'{request.url}\': {exception}')
return abort(500)
user.update(password=request.form.get('password'))
@ -162,7 +162,7 @@ def _update_password():
def _users():
form = CreateUser()
try: users = User.query.all()
except SQLAlchemyError as exception:
except (SQLAlchemyError, ConnectionError) as exception:
write('system.log', f'Database error when processing request \'{request.url}\': {exception}')
return abort(500)
if request.method == 'POST':
@ -182,7 +182,7 @@ def _users():
@login_required
def _delete_user(id:str):
try: user = User.query.filter_by(id=id).first()
except SQLAlchemyError as exception:
except (SQLAlchemyError, ConnectionError) as exception:
write('system.log', f'Database error when processing request \'{request.url}\': {exception}')
return abort(500)
form = DeleteUser()
@ -209,7 +209,7 @@ def _delete_user(id:str):
@login_required
def _update_user(id:str):
try: user = User.query.filter_by(id=id).first()
except SQLAlchemyError as exception:
except (SQLAlchemyError, ConnectionError) as exception:
write('system.log', f'Database error when processing request \'{request.url}\': {exception}')
return abort(500)
form = UpdateUser()
@ -254,7 +254,7 @@ def _questions():
return send_errors_to_client(form=form)
try: data = Dataset.query.all()
except SQLAlchemyError as exception:
except (SQLAlchemyError, ConnectionError) as exception:
write('system.log', f'Database error when processing request \'{request.url}\': {exception}')
return abort(500)
return render_template('/admin/settings/questions.html', form=form, data=data)
@ -266,7 +266,7 @@ def _edit_questions():
action = request.get_json()['action']
if not action == 'delete': return jsonify({'error': 'Invalid action.'}), 400
try: dataset = Dataset.query.filter_by(id=id).first()
except SQLAlchemyError as exception:
except (SQLAlchemyError, ConnectionError) as exception:
write('system.log', f'Database error when processing request \'{request.url}\': {exception}')
return abort(500)
if action == 'delete': success, message = dataset.delete()
@ -277,7 +277,7 @@ def _edit_questions():
@login_required
def _download(id:str):
try: dataset = Dataset.query.filter_by(id=id).first()
except SQLAlchemyError as exception:
except (SQLAlchemyError, ConnectionError) as exception:
write('system.log', f'Database error when processing request \'{request.url}\': {exception}')
return abort(500)
if not dataset: return abort(404)
@ -291,7 +291,7 @@ def _download(id:str):
def _tests(filter:str=None):
tests = None
try: _tests = Test.query.all()
except SQLAlchemyError as exception:
except (SQLAlchemyError, ConnectionError) as exception:
write('system.log', f'Database error when processing request \'{request.url}\': {exception}')
return abort(500)
form = None
@ -340,7 +340,7 @@ def _create_test():
new_test.time_limit = None if request.form.get('time_limit') == 'none' else int(request.form.get('time_limit'))
dataset = request.form.get('dataset')
try: new_test.dataset = Dataset.query.filter_by(id=dataset).first()
except SQLAlchemyError as exception:
except (SQLAlchemyError, ConnectionError) as exception:
write('system.log', f'Database error when processing request \'{request.url}\': {exception}')
return abort(500)
success, message = new_test.create()
@ -357,7 +357,7 @@ def _edit_test():
action = request.get_json()['action']
if action not in ['start', 'delete', 'end']: return jsonify({'error': 'Invalid action.'}), 400
try: test = Test.query.filter_by(id=id).first()
except SQLAlchemyError as exception:
except (SQLAlchemyError, ConnectionError) as exception:
write('system.log', f'Database error when processing request \'{request.url}\': {exception}')
return abort(500)
if not test: return jsonify({'error': 'Could not find the corresponding test to delete.'}), 404
@ -374,7 +374,7 @@ def _edit_test():
def _view_test(id:str=None):
form = AddTimeAdjustment()
try: test = Test.query.filter_by(id=id).first()
except SQLAlchemyError as exception:
except (SQLAlchemyError, ConnectionError) as exception:
write('system.log', f'Database error when processing request \'{request.url}\': {exception}')
return abort(500)
if request.method == 'POST':
@ -394,7 +394,7 @@ def _view_test(id:str=None):
@login_required
def _delete_adjustment(id:str=None):
try: test = Test.query.filter_by(id=id).first()
except SQLAlchemyError as exception:
except (SQLAlchemyError, ConnectionError) as exception:
write('system.log', f'Database error when processing request \'{request.url}\': {exception}')
return abort(500)
if not test: return jsonify({'error': 'Invalid test ID.'}), 404
@ -407,7 +407,7 @@ def _delete_adjustment(id:str=None):
@login_required
def _view_entries():
try: entries = Entry.query.all()
except SQLAlchemyError as exception:
except (SQLAlchemyError, ConnectionError) as exception:
write('system.log', f'Database error when processing request \'{request.url}\': {exception}')
return abort(500)
return render_template('/admin/results.html', entries = entries)
@ -416,7 +416,7 @@ def _view_entries():
@login_required
def _view_entry(id:str=None):
try: entry = Entry.query.filter_by(id=id).first()
except SQLAlchemyError as exception:
except (SQLAlchemyError, ConnectionError) as exception:
write('system.log', f'Database error when processing request \'{request.url}\': {exception}')
return abort(500)
if request.method == 'POST':
@ -450,7 +450,7 @@ def _generate_certificate():
from ..extensions import db
id = request.get_json()['id']
try: entry = Entry.query.filter_by(id=id).first()
except SQLAlchemyError as exception:
except (SQLAlchemyError, ConnectionError) as exception:
write('system.log', f'Database error when processing request \'{request.url}\': {exception}')
return abort(500)
if not entry: return jsonify({'error': 'Invalid entry ID.'}), 404