Displaying the filename when the check-json hook fails with a UnicodeDecodeError - fix #148
diff --git a/pre_commit_hooks/check_json.py b/pre_commit_hooks/check_json.py
index 8328cc7..f586fe9 100644
--- a/pre_commit_hooks/check_json.py
+++ b/pre_commit_hooks/check_json.py
@@ -15,7 +15,7 @@
for filename in args.filenames:
try:
simplejson.load(open(filename))
- except simplejson.JSONDecodeError as exc:
+ except (simplejson.JSONDecodeError, UnicodeDecodeError) as exc:
print('{0}: Failed to json encode ({1})'.format(filename, exc))
retval = 1
return retval
diff --git a/testing/resources/bad_json_latin1.nonjson b/testing/resources/bad_json_latin1.nonjson
new file mode 100755
index 0000000..3b39473
--- /dev/null
+++ b/testing/resources/bad_json_latin1.nonjson
@@ -0,0 +1,3 @@
+{
+ "é": 1,
+}
diff --git a/tests/check_json_test.py b/tests/check_json_test.py
index 18d1b66..996bfbb 100644
--- a/tests/check_json_test.py
+++ b/tests/check_json_test.py
@@ -6,8 +6,12 @@
@pytest.mark.parametrize(('filename', 'expected_retval'), (
('bad_json.notjson', 1),
+ ('bad_json_latin1.nonjson', 1),
('ok_json.json', 0),
))
-def test_check_json(filename, expected_retval):
+def test_check_json(capsys, filename, expected_retval):
ret = check_json([get_resource_path(filename)])
assert ret == expected_retval
+ if expected_retval == 1:
+ stdout, _ = capsys.readouterr()
+ assert filename in stdout