Show more detailed error on invalid JSON, closes #532

This commit is contained in:
Simon Willison 2023-05-08 13:31:56 -07:00
commit e4ed372517
2 changed files with 7 additions and 5 deletions

View file

@ -994,9 +994,11 @@ def insert_upsert_implementation(
docs = json.load(decoded)
if isinstance(docs, dict):
docs = [docs]
except json.decoder.JSONDecodeError:
except json.decoder.JSONDecodeError as ex:
raise click.ClickException(
"Invalid JSON - use --csv for CSV or --tsv for TSV files"
"Invalid JSON - use --csv for CSV or --tsv for TSV files\n\nJSON error: {}".format(
ex
)
)
if flatten:
docs = (_flatten(doc) for doc in docs)

View file

@ -43,9 +43,9 @@ def test_insert_invalid_json_error(tmpdir):
input="name,age\nCleo,4",
)
assert result.exit_code == 1
assert (
result.output
== "Error: Invalid JSON - use --csv for CSV or --tsv for TSV files\n"
assert result.output == (
"Error: Invalid JSON - use --csv for CSV or --tsv for TSV files\n\n"
"JSON error: Expecting value: line 1 column 1 (char 0)\n"
)