lacre.admin: Handle database exceptions
This commit is contained in:
parent
b44bd7b150
commit
0975ce3a69
|
@ -21,6 +21,11 @@ import lacre.dbschema as db
|
|||
LOG = logging.getLogger('lacre.admin')
|
||||
|
||||
|
||||
def _no_database():
|
||||
print('Database unavailable or not configured properly')
|
||||
exit(lacre.EX_CONFIG)
|
||||
|
||||
|
||||
def sub_queue(args):
|
||||
"""Sub-command to inspect queue contents."""
|
||||
LOG.debug('Inspecting queue...')
|
||||
|
@ -30,6 +35,9 @@ def sub_queue(args):
|
|||
|
||||
cnt = queue.count_keys()
|
||||
|
||||
if cnt is None:
|
||||
_no_database()
|
||||
|
||||
print(f'Keys in the queue: {cnt}')
|
||||
|
||||
|
||||
|
@ -41,6 +49,9 @@ def sub_identities(args):
|
|||
identities = repo.IdentityRepository(conn)
|
||||
|
||||
all_identities = identities.freeze_identities()
|
||||
if all_identities is None:
|
||||
_no_database()
|
||||
|
||||
if args.email:
|
||||
all_rev = all_identities.emails()
|
||||
print('-', args.email, all_rev[args.email])
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
"""Lacre identity and key repositories."""
|
||||
|
||||
from sqlalchemy import create_engine, select, delete, and_, func
|
||||
from sqlalchemy.exc import OperationalError
|
||||
import logging
|
||||
|
||||
from lacre._keyringcommon import KeyRing, KeyCache
|
||||
|
@ -73,7 +74,11 @@ class IdentityRepository(KeyRing):
|
|||
def freeze_identities(self) -> KeyCache:
|
||||
"""Return a static, async-safe copy of the identity map."""
|
||||
self._ensure_connected()
|
||||
return self._load_identities()
|
||||
try:
|
||||
return self._load_identities()
|
||||
except OperationalError:
|
||||
LOG.exception('Cannot retrieve identities')
|
||||
return None
|
||||
|
||||
def _load_identities(self) -> KeyCache:
|
||||
all_identities = select(self._identities.c.fingerprint, self._identities.c.email)
|
||||
|
@ -107,10 +112,14 @@ class KeyConfirmationQueue:
|
|||
selq = select(func.count(self._keys.c.id))
|
||||
|
||||
LOG.debug('Counting all keys: %s', selq)
|
||||
c = [cnt for cnt in self._conn.execute(selq)]
|
||||
try:
|
||||
c = [cnt for cnt in self._conn.execute(selq)]
|
||||
|
||||
# Result is an iterable of tuples:
|
||||
return c[0][0]
|
||||
# Result is an iterable of tuples:
|
||||
return c[0][0]
|
||||
except OperationalError:
|
||||
LOG.exception('Cannot count keys')
|
||||
return None
|
||||
|
||||
def fetch_keys_to_delete(self):
|
||||
seldel = select(self._keys.c.email, self._keys.c.id).where(self._keys.c.status == db.ST_TO_BE_DELETED).limit(self.keys_read_max)
|
||||
|
|
Loading…
Reference in New Issue