From 4a4e38d0a20f7da7cfd91712e7412158e5432cc4 Mon Sep 17 00:00:00 2001 From: ikatyal2110 Date: Thu, 16 Jul 2026 14:26:18 +0000 Subject: [PATCH] Catch runtime exceptions from --code, not just SyntaxError exec() inside _rows_from_code() only caught SyntaxError, so any runtime exception raised by user code (ValueError, NameError, etc.) propagated as an unhandled exception with no output to the user. Now catches Exception so all errors produce a clean "Error in --code:" message consistent with the existing SyntaxError handling. --- sqlite_utils/cli.py | 2 +- tests/test_cli_insert.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/sqlite_utils/cli.py b/sqlite_utils/cli.py index e0b896985..0b42dfbfe 100644 --- a/sqlite_utils/cli.py +++ b/sqlite_utils/cli.py @@ -3800,7 +3800,7 @@ def _rows_from_code(code): namespace = {} try: exec(code, namespace) - except SyntaxError as ex: + except Exception as ex: raise click.ClickException("Error in --code: {}".format(ex)) rows = namespace.get("rows") if callable(rows): diff --git a/tests/test_cli_insert.py b/tests/test_cli_insert.py index df6f80c62..20088d1d3 100644 --- a/tests/test_cli_insert.py +++ b/tests/test_cli_insert.py @@ -880,6 +880,17 @@ def test_insert_code_syntax_error(tmpdir): assert "Error in --code" in result.output +def test_insert_code_runtime_error(tmpdir): + db_path = str(tmpdir / "dogs.db") + result = CliRunner().invoke( + cli.cli, + ["insert", db_path, "creatures", "--code", 'raise ValueError("bad data")'], + ) + assert result.exit_code == 1 + assert "Error in --code" in result.output + assert "bad data" in result.output + + def test_insert_code_file_not_found(tmpdir): db_path = str(tmpdir / "dogs.db") result = CliRunner().invoke(