Exception handling for database queries
This commit is contained in:
@ -1,18 +1,23 @@
|
||||
from .data import load
|
||||
from ..models import User
|
||||
from ..tools.logs import write
|
||||
|
||||
from flask import abort, redirect
|
||||
from flask.helpers import flash, url_for
|
||||
from flask.helpers import abort, flash, redirect, url_for
|
||||
from flask_login import current_user
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
from functools import wraps
|
||||
|
||||
def require_account_creation(function):
|
||||
@wraps(function)
|
||||
def wrapper(*args, **kwargs):
|
||||
if User.query.count() == 0:
|
||||
flash('Please register a user account.', 'alert')
|
||||
return redirect(url_for('admin._register'))
|
||||
try:
|
||||
if User.query.count() == 0:
|
||||
flash('Please register a user account.', 'alert')
|
||||
return redirect(url_for('admin._register'))
|
||||
except SQLAlchemyError as exception:
|
||||
write('system.log', f'Database error when checking for existing accounts: {exception}')
|
||||
return abort(500)
|
||||
return function(*args, **kwargs)
|
||||
return wrapper
|
||||
|
||||
|
@ -1,8 +1,9 @@
|
||||
from ..models import Dataset
|
||||
from ..tools.logs import write
|
||||
|
||||
from flask import current_app as app
|
||||
from flask import flash, redirect
|
||||
from flask.helpers import url_for
|
||||
from flask.helpers import abort, flash, redirect, url_for
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
@ -76,7 +77,10 @@ def get_tag_list(dataset:list):
|
||||
def check_dataset_exists(function):
|
||||
@wraps(function)
|
||||
def wrapper(*args, **kwargs):
|
||||
datasets = Dataset.query.all()
|
||||
try: datasets = Dataset.query.all()
|
||||
except SQLAlchemyError as exception:
|
||||
write('system.log', f'Database error when checking existing datasets: {exception}')
|
||||
return abort(500)
|
||||
if not datasets:
|
||||
flash('There are no available question datasets. Please upload a question dataset first, or use the question editor to create a new dataset.', 'error')
|
||||
return redirect(url_for('admin._questions'))
|
||||
|
@ -1,7 +1,9 @@
|
||||
|
||||
from ..extensions import db
|
||||
from ..tools.logs import write
|
||||
|
||||
from flask import jsonify
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from wtforms.validators import ValidationError
|
||||
|
||||
import json
|
||||
@ -47,7 +49,10 @@ def get_time_options():
|
||||
|
||||
def get_dataset_choices():
|
||||
from ..models import Dataset
|
||||
datasets = Dataset.query.all()
|
||||
try: datasets = Dataset.query.all()
|
||||
except SQLAlchemyError as exception:
|
||||
write('system.log', f'Database error when fetching dataset lists: {exception}')
|
||||
return []
|
||||
dataset_choices = []
|
||||
for dataset in datasets:
|
||||
label = dataset.get_name()
|
||||
|
@ -1,8 +1,10 @@
|
||||
from .data import randomise_list
|
||||
from ..models import Entry
|
||||
from ..tools.logs import write
|
||||
|
||||
from flask import redirect, request, session
|
||||
from flask.helpers import url_for
|
||||
from flask import request, session
|
||||
from flask.helpers import abort, redirect, url_for
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
from functools import wraps
|
||||
|
||||
@ -129,8 +131,11 @@ def redirect_if_started(function):
|
||||
@wraps(function)
|
||||
def wrapper(*args, **kwargs):
|
||||
id = session.get('id')
|
||||
if request.method == 'GET' and id and Entry.query.filter_by(id=id).first():
|
||||
return redirect(url_for('quiz._quiz'))
|
||||
try:
|
||||
if request.method == 'GET' and id and Entry.query.filter_by(id=id).first(): return redirect(url_for('quiz._quiz'))
|
||||
except SQLAlchemyError as exception:
|
||||
write('system.log', f'Database error when checking if test has been started: {exception}')
|
||||
return abort(500)
|
||||
return function(*args, **kwargs)
|
||||
return wrapper
|
||||
|
||||
|
Reference in New Issue
Block a user