From c2b4740029ed0660efea82e7042e0d7cb9134b10 Mon Sep 17 00:00:00 2001 From: Remi Gau Date: Mon, 16 Dec 2024 11:30:24 +0100 Subject: [PATCH] graceful error when failing to load invalid yml --- .../schemacode/src/bidsschematools/types/namespace.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tools/schemacode/src/bidsschematools/types/namespace.py b/tools/schemacode/src/bidsschematools/types/namespace.py index e2c37eee40..73b06ed060 100644 --- a/tools/schemacode/src/bidsschematools/types/namespace.py +++ b/tools/schemacode/src/bidsschematools/types/namespace.py @@ -272,10 +272,13 @@ def from_json(cls, jsonstr: str): def _read_yaml_dir(path: Path) -> dict: mapping = {} for subpath in sorted(path.iterdir()): - if subpath.is_dir(): - mapping[subpath.name] = _read_yaml_dir(subpath) - elif subpath.name.endswith("yaml"): - mapping[subpath.stem] = yaml.safe_load(subpath.read_text()) + try: + if subpath.is_dir(): + mapping[subpath.name] = _read_yaml_dir(subpath) + elif subpath.name.endswith("yaml"): + mapping[subpath.stem] = yaml.safe_load(subpath.read_text()) + except Exception as e: + raise ValueError(f"There was an error reading the file: {subpath}") from e return mapping