Verified Commit 6e821be6 authored by David Runge's avatar David Runge
Browse files

Improve Exception handling in convert

repo_management/convert.py:
Change the docuemntation of `_desc_data_line_to_dicts()` to reflect,
that it may raise ValueError under certain conditions.
Change `_desc_data_to_model()` to try/except ValueError raised by
`_desc_data_line_to_dicts()` and raise a
`errors.REpomanagementValidationError` instead.

tests/test_convert.py:
Change tests expecting to raise `ValueError` to expect
`errors.RepoManagementValidationError` instead.
parent ac42d90a
......@@ -70,6 +70,11 @@ def _desc_data_line_to_dicts(
A dict for instances of type string
int_types: Dict[str, int]
A dict for instances of type int
Raises
------
ValueError
If a string is provided for a field of type int, that can not be cast to type int
"""
if current_type == defaults.FieldType.STRING_LIST:
......@@ -93,10 +98,9 @@ def _desc_data_to_model(data: io.StringIO) -> models.PackageDesc:
Raises
------
ValueError
If a string is provided for a field of type int, that can not be cast to type int
pydantic.error_wrappers.ValidationError
If a required field is missing
errors.RepoManagementValidationError
If a pydantic.error_wrappers.ValidationError is raised (e.g. due to a missing attribute) or if a ValueError is
raised when converting data (e.g. when calling _desc_data_line_to_dicts())
Returns
-------
......@@ -121,6 +125,7 @@ def _desc_data_to_model(data: io.StringIO) -> models.PackageDesc:
continue
if current_header:
try:
_desc_data_line_to_dicts(
current_header=current_header,
current_type=current_type,
......@@ -129,13 +134,17 @@ def _desc_data_to_model(data: io.StringIO) -> models.PackageDesc:
string_types=string_types,
int_types=int_types,
)
except ValueError as e:
raise errors.RepoManagementValidationError(
f"A validation error occured while creating the file:\n\n{data.getvalue()}\n{e}"
)
merged_dict: Dict[str, Union[int, str, List[str]]] = {**int_types, **string_types, **string_list_types}
try:
return models.PackageDesc(**merged_dict)
except ValidationError as e:
raise errors.RepoManagementValidationError(
f"An error occured while validating the file: {data.getvalue()}\n{e}"
f"A validation error occured while creating the file:\n\n{data.getvalue()}\n{e}"
)
......
......@@ -81,7 +81,7 @@ def test__files_data_to_dict(
"%PGPSIG%\nfoo\n%PROVIDES%\nfoo\nbar\n%REPLACES%\nfoo\nbar\n"
"%SHA256SUM%\nfoo\n%URL%\nfoo\n%VERSION%\nfoo\n"
),
raises(ValueError),
raises(errors.RepoManagementValidationError),
),
("%FOO%\nbar\n", raises(errors.RepoManagementValidationError)),
(
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment