From a096cc81cfa65a166127a3a7e0942c277fbb66b8 Mon Sep 17 00:00:00 2001 From: Wolfgang Müller Date: Mon, 3 May 2021 22:14:29 +0200 Subject: main: Handle SQLAlchemy errors more nicely Instead of giving the user a screen full of backtraces, catch any errors SQLAlchemy can run into when preparing or executing the query and report only the error message to the user. We include the print loop in the try/except block because an upcoming commit will add a handler for KeyboardInterrupt. --- quarg/main.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/quarg/main.py b/quarg/main.py index ce72310..9532ba7 100644 --- a/quarg/main.py +++ b/quarg/main.py @@ -4,13 +4,13 @@ import os import sys from timeit import default_timer as timer -from sqlalchemy import create_engine +from sqlalchemy import create_engine, exc from sqlalchemy.orm import sessionmaker import quarg.actions as actions import quarg.database.filters as filters +import quarg.quassel.formatter as formatter from quarg.database.tables import Backlog, Buffer, Network, QuasselUser, Sender -from quarg.quassel.formatter import format_from from quarg.utils import errx # pylint: disable=line-too-long @@ -136,11 +136,14 @@ def main(): if not predicates: errx('Nothing to match.') - query = prepare_query(session, predicates, args) - rows, time = time_query(query) + try: + query = prepare_query(session, predicates, args) + rows, time = time_query(query) - for row in rows: - print(format_from(row)) + for row in rows: + print(formatter.format_from(row)) + except exc.SQLAlchemyError as err: + errx(err) print(f'Query returned {len(rows)} lines in {time:.4f} seconds.', file=sys.stderr) -- cgit v1.2.3-2-gb3c3