Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion json5/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ def main(argv=None, host=None):
args.trailing_commas = False
args.quote_style = json5.QuoteStyle.ALWAYS_DOUBLE.value

obj = json5.loads(inp, strict=args.strict)
try:
obj = json5.loads(inp, strict=args.strict)
except ValueError as error:
host.print(str(error), file=host.stderr)
return 1

try:
s = json5.dumps(
obj,
Expand Down
24 changes: 24 additions & 0 deletions tests/tool_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,30 @@ def test_as_json_nonfinite(self):
)
self.check(['-c', 'NaN'], out='NaN\n')

def test_invalid_input(self):
# Parse errors should print a clean message like the dump errors
# do, not a traceback.
self.check(
['-c', '{bad'],
returncode=1,
out='',
err='<string>:1 Unexpected end of input at column 5\n',
)
self.check(
[],
stdin='{oops',
returncode=1,
out='',
err='<string>:1 Unexpected end of input at column 6\n',
)
self.check(
[],
stdin='',
returncode=1,
out='',
err='Empty strings are not legal JSON5\n',
)

def test_quote_keys(self):
self.check(
['--quote-keys', '-c', '{foo: 1}'],
Expand Down