Skip to content

Commit bc0ea9a

Browse files
committed
Backport gh-796
FIX: Catch UnicodeDecodeErrors along with JSONDecodeErrors for better reporting Update bids/layout/validation.py
1 parent a4ec1ae commit bc0ea9a

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

bids/layout/index.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def load_json(path):
267267
with open(path, 'r', encoding='utf-8') as handle:
268268
try:
269269
return json.load(handle)
270-
except json.JSONDecodeError as e:
270+
except (UnicodeDecodeError, json.JSONDecodeError) as e:
271271
msg = f"Error occurred while trying to decode JSON from file {path}"
272272
raise IOError(msg) from e
273273

bids/layout/validation.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,13 @@ def validate_root(root, validate):
7979
else:
8080
description = None
8181
else:
82+
err = None
8283
try:
8384
with open(target, 'r', encoding='utf-8') as desc_fd:
8485
description = json.load(desc_fd)
85-
except json.JSONDecodeError:
86+
except (UnicodeDecodeError, json.JSONDecodeError) as e:
8687
description = None
88+
err = e
8789
if validate:
8890

8991
if description is None:
@@ -92,7 +94,7 @@ def validate_root(root, validate):
9294
" There is likely a typo in your 'dataset_description.json'."
9395
"\nExample contents of 'dataset_description.json': \n%s" %
9496
json.dumps(EXAMPLE_BIDS_DESCRIPTION)
95-
)
97+
) from err
9698

9799
for k in MANDATORY_BIDS_FIELDS:
98100
if k not in description:

0 commit comments

Comments
 (0)