Exception handling for database queries

This commit is contained in:
2022-08-20 10:56:43 +01:00
parent b8fd65d856
commit 866c9b10cf
15 changed files with 234 additions and 114 deletions

View File

@@ -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