From 79f00721e31b53685ee1e48ea9c7afe75a162ef6 Mon Sep 17 00:00:00 2001 From: Markus Rye Foss <8709797+markusrf@users.noreply.github.com> Date: Tue, 26 Nov 2024 13:11:37 +0100 Subject: [PATCH] fix: raise error if yaml file is empty or not a valid yaml object (#712) --- src/libecalc/presentation/yaml/model.py | 20 +++++++++++++++++-- .../yaml/yaml_models/pyyaml_yaml_model.py | 11 ++++++++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/libecalc/presentation/yaml/model.py b/src/libecalc/presentation/yaml/model.py index 23842c60d7..c4ec93ebd7 100644 --- a/src/libecalc/presentation/yaml/model.py +++ b/src/libecalc/presentation/yaml/model.py @@ -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, @@ -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 diff --git a/src/libecalc/presentation/yaml/yaml_models/pyyaml_yaml_model.py b/src/libecalc/presentation/yaml/yaml_models/pyyaml_yaml_model.py index 1944c7f0ad..6dacacd4ff 100644 --- a/src/libecalc/presentation/yaml/yaml_models/pyyaml_yaml_model.py +++ b/src/libecalc/presentation/yaml/yaml_models/pyyaml_yaml_model.py @@ -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 @@ -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"):