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.
This commit is contained in:
ikatyal2110 2026-07-16 14:26:18 +00:00
commit 4a4e38d0a2
No known key found for this signature in database
2 changed files with 12 additions and 1 deletions

View file

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

View file

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