Skip to content

Commit 1752d34

Browse files
authored
Merge pull request #549 from OpenCOMPES/pydantic-error-handling
Pydantic error handling
2 parents c333747 + 2c2a9ae commit 1752d34

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/sed/core/config.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import yaml
1313
from platformdirs import user_config_path
14+
from pydantic import ValidationError
1415

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

147149
if not verify_config:
148150
return config_dict
149-
# Run the config through the ConfigModel to ensure it is valid
150-
config_model = ConfigModel(**config_dict)
151-
return config_model.model_dump(exclude_unset=True, exclude_none=True)
151+
152+
try:
153+
# Run the config through the ConfigModel to ensure it is valid
154+
config_model = ConfigModel(**config_dict)
155+
return config_model.model_dump(exclude_unset=True, exclude_none=True)
156+
except ValidationError as e:
157+
error_msg = (
158+
"Invalid configuration file detected. The following validation errors were found:\n"
159+
)
160+
for error in e.errors():
161+
error_msg += f"\n- {' -> '.join(str(loc) for loc in error['loc'])}: {error['msg']}"
162+
logger.error(error_msg)
163+
raise ValueError(error_msg) from e
152164

153165

154166
def load_config(config_path: str) -> dict:

tests/test_config.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from pathlib import Path
99

1010
import pytest
11-
from pydantic import ValidationError
1211

1312
from sed.core.config import complete_dictionary
1413
from sed.core.config import load_config
@@ -171,7 +170,7 @@ def test_invalid_config_extra_field():
171170
)
172171
invalid_config = default_config.copy()
173172
invalid_config["extra_field"] = "extra_value"
174-
with pytest.raises(ValidationError):
173+
with pytest.raises(ValueError):
175174
parse_config(
176175
invalid_config,
177176
folder_config={},
@@ -191,7 +190,7 @@ def test_invalid_config_missing_field():
191190
)
192191
invalid_config = default_config.copy()
193192
del invalid_config["core"]["loader"]
194-
with pytest.raises(ValidationError):
193+
with pytest.raises(ValueError):
195194
parse_config(
196195
folder_config={},
197196
user_config={},
@@ -211,7 +210,7 @@ def test_invalid_config_wrong_values():
211210
)
212211
invalid_config = default_config.copy()
213212
invalid_config["core"]["loader"] = "nonexistent"
214-
with pytest.raises(ValidationError) as e:
213+
with pytest.raises(ValueError) as e:
215214
parse_config(
216215
folder_config={},
217216
user_config={},
@@ -225,7 +224,7 @@ def test_invalid_config_wrong_values():
225224
invalid_config["core"]["copy_tool"]["source"] = "./"
226225
invalid_config["core"]["copy_tool"]["dest"] = "./"
227226
invalid_config["core"]["copy_tool"]["gid"] = 9999
228-
with pytest.raises(ValidationError) as e:
227+
with pytest.raises(ValueError) as e:
229228
parse_config(
230229
folder_config={},
231230
user_config={},

0 commit comments

Comments
 (0)