Commit 55fc9bc0 authored by Erxleben, Fredo's avatar Erxleben, Fredo
Browse files

Improve error handling

parent 80497dd2
Pipeline #90760 passed with stages
in 3 minutes and 58 seconds
......@@ -28,7 +28,7 @@ functions.
.. moduleauthor:: HIFIS Software <software@hifis.net>
"""
import logging
from logging import warning
from logging import warning, debug
from typing import Dict, List, Set, Union
import pandas
......@@ -126,6 +126,7 @@ class DataContainer(object):
"Attempt to add QuestionCollection " "with duplicate ID"
)
self._survey_questions[new_collection.full_id] = new_collection
debug(f"{new_collection.full_id} added successfully")
def load_survey_data(self, csv_data: List[List[str]]) -> None:
"""
......@@ -165,7 +166,10 @@ class DataContainer(object):
for (question_index, question) in question_cache.items():
answer: str = row[question_index]
question.add_answer(participant_id, answer)
try:
question.add_answer(participant_id, answer)
except (KeyError, ValueError) as error:
warning(f"When loading CSV data: {error}")
def collection_for_id(self, full_id: str) -> QuestionCollection:
"""
......
......@@ -85,9 +85,15 @@ class Identifiable(object):
The used ID will be removed from the known IDs and can be re-used.
"""
assert self._full_id in Identifiable.known_ids
Identifiable.known_ids.remove(self._full_id)
try:
Identifiable.known_ids.remove(self._full_id)
# FIXME For some reason removing the full ID from the list of
# known IDs fails due to them already being removed. But why?
# This has been put into this little exception-catch box to not
# spam the command line output, but I would prefer to understand
# better what is going on here…
except KeyError:
pass
@property
def short_id(self) -> str:
......
......@@ -28,7 +28,7 @@ structure of the YAML to be parsed.
from abc import ABC, abstractmethod
from typing import Dict, List, Union
from schema import Or, Schema
from schema import Or, Schema, SchemaError
# A shorthand type for the kind of lists and dictionaries that can be
# encountered when parsing YAML data
......@@ -85,5 +85,20 @@ class YamlConstructable(ABC):
A new instance of the overriding subclass
"""
schema: Schema = cls.schema
validated_yaml = schema.validate(yaml)
return cls._from_yaml_dictionary(yaml=validated_yaml, **kwargs)
try:
validated_yaml = schema.validate(yaml)
return cls._from_yaml_dictionary(yaml=validated_yaml, **kwargs)
except SchemaError as validation_error:
# Construct a reduces YAML representation to limit the output in
# the error message to the essentials.
# This generator expression basically constructs a copy of the
# top YAML object, but all values that are either lists or
# dictionaries themselves will be replaced by the text "…"
reduced_yaml = {
key: (
"…" if isinstance(value, dict) or isinstance(value, list)
else value
)
for (key, value) in yaml.items()
}
raise ValueError(f"{validation_error} when parsing {reduced_yaml}")
Supports Markdown
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