From e4ed37251746b25ca69b5ace0c8c7992024556df Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Mon, 8 May 2023 13:31:56 -0700 Subject: [PATCH] Show more detailed error on invalid JSON, closes #532 --- sqlite_utils/cli.py | 6 ++++-- tests/test_cli_insert.py | 6 +++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/sqlite_utils/cli.py b/sqlite_utils/cli.py index ba0a416..c3c55e2 100644 --- a/sqlite_utils/cli.py +++ b/sqlite_utils/cli.py @@ -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) diff --git a/tests/test_cli_insert.py b/tests/test_cli_insert.py index 3c3b88d..f403328 100644 --- a/tests/test_cli_insert.py +++ b/tests/test_cli_insert.py @@ -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" )