Better error messages in CLI, closes #309

This commit is contained in:
Simon Willison 2021-08-09 15:25:52 -07:00
commit 14f643d9e9
2 changed files with 48 additions and 4 deletions

View file

@ -786,12 +786,25 @@ def insert_upsert_implementation(
db[table].insert_all(
docs, pk=pk, batch_size=batch_size, alter=alter, **extra_kwargs
)
except sqlite3.OperationalError as e:
if e.args and "has no column named" in e.args[0]:
except Exception as e:
if (
isinstance(e, sqlite3.OperationalError)
and e.args
and "has no column named" in e.args[0]
):
raise click.ClickException(
"{}\n\nTry using --alter to add additional columns".format(e.args[0])
)
raise
# If we can find sql= and params= arguments, show those
variables = _find_variables(e.__traceback__, ["sql", "params"])
if "sql" in variables and "params" in variables:
raise click.ClickException(
"{}\n\nsql = {}\nparams={}".format(
str(e), variables["sql"], variables["params"]
)
)
else:
raise
if tracker is not None:
db[table].transform(types=tracker.types)
@ -805,6 +818,18 @@ def _flatten(d):
yield key, value
def _find_variables(tb, vars):
to_find = list(vars)
found = {}
for var in to_find:
if var in tb.tb_frame.f_locals:
vars.remove(var)
found[var] = tb.tb_frame.f_locals[var]
if vars and tb.tb_next:
found.update(_find_variables(tb.tb_next, vars))
return found
@cli.command()
@insert_upsert_options
@click.option(