mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 17:34:32 +02:00
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:
parent
a947dc6739
commit
4a4e38d0a2
2 changed files with 12 additions and 1 deletions
|
|
@ -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):
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue