Skip to content

Commit

Permalink
Merge pull request #549 from OpenCOMPES/pydantic-error-handling
Browse files Browse the repository at this point in the history
Pydantic error handling
  • Loading branch information
rettigl authored Jan 16, 2025
2 parents c333747 + 2c2a9ae commit 1752d34
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
18 changes: 15 additions & 3 deletions src/sed/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import yaml
from platformdirs import user_config_path
from pydantic import ValidationError

from sed.core.config_model import ConfigModel
from sed.core.logging import setup_logging
Expand Down Expand Up @@ -61,6 +62,7 @@ def parse_config(
Raises:
TypeError: Raised if the provided file is neither *json* nor *yaml*.
FileNotFoundError: Raised if the provided file is not found.
ValueError: Raised if there is a validation error in the config file.
Returns:
dict: Loaded and completed config dict, possibly verified by pydantic config model.
Expand Down Expand Up @@ -146,9 +148,19 @@ def parse_config(

if not verify_config:
return config_dict
# Run the config through the ConfigModel to ensure it is valid
config_model = ConfigModel(**config_dict)
return config_model.model_dump(exclude_unset=True, exclude_none=True)

try:
# Run the config through the ConfigModel to ensure it is valid
config_model = ConfigModel(**config_dict)
return config_model.model_dump(exclude_unset=True, exclude_none=True)
except ValidationError as e:
error_msg = (
"Invalid configuration file detected. The following validation errors were found:\n"
)
for error in e.errors():
error_msg += f"\n- {' -> '.join(str(loc) for loc in error['loc'])}: {error['msg']}"
logger.error(error_msg)
raise ValueError(error_msg) from e


def load_config(config_path: str) -> dict:
Expand Down
9 changes: 4 additions & 5 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from pathlib import Path

import pytest
from pydantic import ValidationError

from sed.core.config import complete_dictionary
from sed.core.config import load_config
Expand Down Expand Up @@ -171,7 +170,7 @@ def test_invalid_config_extra_field():
)
invalid_config = default_config.copy()
invalid_config["extra_field"] = "extra_value"
with pytest.raises(ValidationError):
with pytest.raises(ValueError):
parse_config(
invalid_config,
folder_config={},
Expand All @@ -191,7 +190,7 @@ def test_invalid_config_missing_field():
)
invalid_config = default_config.copy()
del invalid_config["core"]["loader"]
with pytest.raises(ValidationError):
with pytest.raises(ValueError):
parse_config(
folder_config={},
user_config={},
Expand All @@ -211,7 +210,7 @@ def test_invalid_config_wrong_values():
)
invalid_config = default_config.copy()
invalid_config["core"]["loader"] = "nonexistent"
with pytest.raises(ValidationError) as e:
with pytest.raises(ValueError) as e:
parse_config(
folder_config={},
user_config={},
Expand All @@ -225,7 +224,7 @@ def test_invalid_config_wrong_values():
invalid_config["core"]["copy_tool"]["source"] = "./"
invalid_config["core"]["copy_tool"]["dest"] = "./"
invalid_config["core"]["copy_tool"]["gid"] = 9999
with pytest.raises(ValidationError) as e:
with pytest.raises(ValueError) as e:
parse_config(
folder_config={},
user_config={},
Expand Down

0 comments on commit 1752d34

Please sign in to comment.