Skip to content

Pydantic error handling #549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading