Skip to content

Commit

Permalink
Catch config parse error
Browse files Browse the repository at this point in the history
  • Loading branch information
gmertes committed Jun 26, 2024
1 parent 60733b7 commit a534152
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/anemoi/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,21 @@ def config_path(name="settings.toml"):


def _load(path):
if path.endswith(".json"):
with open(path, "rb") as f:
return json.load(f)

if path.endswith(".yaml") or path.endswith(".yml"):
with open(path, "rb") as f:
return yaml.safe_load(f)

if path.endswith(".toml"):
with open(path, "rb") as f:
return tomllib.load(f)
try:
if path.endswith(".json"):
with open(path, "rb") as f:
return json.load(f)

if path.endswith(".yaml") or path.endswith(".yml"):
with open(path, "rb") as f:
return yaml.safe_load(f)

if path.endswith(".toml"):
with open(path, "rb") as f:
return tomllib.load(f)
except (json.JSONDecodeError, yaml.YAMLError, tomllib.TOMLDecodeError) as e:
LOG.warning(f"Failed to parse config file {path}", exc_info=e)
return {}

return open(path).read()

Expand Down

0 comments on commit a534152

Please sign in to comment.