Skip to content

Commit

Permalink
fix: raise error if yaml file is empty or not a valid yaml object (#712)
Browse files Browse the repository at this point in the history
  • Loading branch information
markusrf authored Nov 26, 2024
1 parent c463ba8 commit 79f0072
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
20 changes: 18 additions & 2 deletions src/libecalc/presentation/yaml/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
from libecalc.presentation.yaml.mappers.variables_mapper.get_global_time_vector import get_global_time_vector
from libecalc.presentation.yaml.model_validation_exception import ModelValidationException
from libecalc.presentation.yaml.resource_service import ResourceService
from libecalc.presentation.yaml.validation_errors import DtoValidationError
from libecalc.presentation.yaml.validation_errors import DtoValidationError, Location, ModelValidationError
from libecalc.presentation.yaml.yaml_models.exceptions import DuplicateKeyError, YamlError
from libecalc.presentation.yaml.yaml_models.yaml_model import YamlValidator
from libecalc.presentation.yaml.yaml_validation_context import (
ModelContext,
Expand Down Expand Up @@ -54,7 +55,22 @@ def __init__(
output_frequency: Frequency,
) -> None:
self._output_frequency = output_frequency
self._configuration = configuration_service.get_configuration()
try:
self._configuration = configuration_service.get_configuration()
except YamlError as e:
location = Location(keys=[])
if isinstance(e, DuplicateKeyError):
location = Location(keys=[e.key])
raise ModelValidationException(
errors=[
ModelValidationError(
location=location,
message=e.problem,
file_context=e.file_context,
data=None,
)
]
) from e
self.resources = resource_service.get_resources(self._configuration)

self._is_validated = False
Expand Down
11 changes: 9 additions & 2 deletions src/libecalc/presentation/yaml/yaml_models/pyyaml_yaml_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
from pydantic import TypeAdapter
from pydantic import ValidationError as PydanticValidationError
from typing_extensions import deprecated
from yaml import SafeLoader
from yaml import (
SafeLoader,
)

from libecalc.common.errors.exceptions import ProgrammingError
from libecalc.common.time_utils import convert_date_to_datetime
Expand Down Expand Up @@ -234,13 +236,18 @@ def read_yaml(
resources: Optional[dict[str, TextIO]] = None,
) -> YamlDict:
try:
return PyYamlYamlModel._read_yaml_helper(
read_yaml = PyYamlYamlModel._read_yaml_helper(
yaml_file=main_yaml,
loader=PyYamlYamlModel.SafeLineLoader,
enable_include=enable_include,
base_dir=base_dir,
resources=resources,
)
if read_yaml is None:
raise yaml.YAMLError("YAML is empty")
if not isinstance(read_yaml, dict):
raise yaml.YAMLError("Not a valid YAML object")
return read_yaml
except yaml.YAMLError as e:
file_context = None
if hasattr(e, "problem_mark"):
Expand Down

0 comments on commit 79f0072

Please sign in to comment.