From 287cdcae8908916687f2ecccc87c38549d004ac6 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Tue, 15 Jun 2021 21:40:28 -0700 Subject: [PATCH] Turn SQL errors into click errors --- sqlite_utils/cli.py | 5 ++++- tests/test_cli.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/sqlite_utils/cli.py b/sqlite_utils/cli.py index 6c40454..4208830 100644 --- a/sqlite_utils/cli.py +++ b/sqlite_utils/cli.py @@ -1088,7 +1088,10 @@ def query( _load_extensions(db, load_extension) db.register_fts4_bm25() with db.conn: - cursor = db.execute(sql, dict(param)) + try: + cursor = db.execute(sql, dict(param)) + except sqlite3.OperationalError as e: + raise click.ClickException(str(e)) if cursor.description is None: # This was an update/insert headers = ["rows_affected"] diff --git a/tests/test_cli.py b/tests/test_cli.py index a73cdff..5cb4af0 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1049,7 +1049,7 @@ def test_query_load_extension(use_spatialite_shortcut): # Without --load-extension: result = CliRunner().invoke(cli.cli, [":memory:", "select spatialite_version()"]) assert result.exit_code == 1 - assert "no such function: spatialite_version" in repr(result) + assert "no such function: spatialite_version" in result.output # With --load-extension: if use_spatialite_shortcut: load_extension = "spatialite"