From 727b611e602231de820f1e523305ba44372e1e94 Mon Sep 17 00:00:00 2001 From: Vasek Sraier Date: Fri, 11 Mar 2022 12:28:24 +0100 Subject: [PATCH] manager: improved formatting of errors --- .../datamodel/types/base_types.py | 16 ++++++---------- manager/knot_resolver_manager/exceptions.py | 3 ++- .../kresd_controller/systemd/dbus_api.py | 9 +++++---- manager/knot_resolver_manager/utils/modelling.py | 4 +++- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/manager/knot_resolver_manager/datamodel/types/base_types.py b/manager/knot_resolver_manager/datamodel/types/base_types.py index b168fbc44..7dc203a7e 100644 --- a/manager/knot_resolver_manager/datamodel/types/base_types.py +++ b/manager/knot_resolver_manager/datamodel/types/base_types.py @@ -76,17 +76,16 @@ class IntRangeBase(IntBase): if isinstance(source_value, int) and not isinstance(source_value, bool): if hasattr(self, "_min") and (source_value < self._min): raise SchemaException( - f"The value {source_value} is lower than the allowed minimum {self._min}.", object_path + f"value {source_value} is lower than the minimum {self._min}.", object_path ) if hasattr(self, "_max") and (source_value > self._max): raise SchemaException( - f"The value {source_value} is higher than the allowed maximum {self._max}", object_path + f"value {source_value} is higher than the maximum {self._max}", object_path ) self._value = source_value else: raise SchemaException( - f"Unexpected input type for integer - {type(source_value)}." - " Cause might be invalid format or invalid type.", + f"expected integer, got '{type(source_value)}'", object_path, ) @@ -120,8 +119,7 @@ class PatternBase(StrBase): raise SchemaException(f"'{source_value}' does not match '{self._re.pattern}' pattern", object_path) else: raise SchemaException( - f"Unexpected input type for string pattern - {type(source_value)}." - " Cause might be invalid format or invalid type.", + f"expected string, got '{type(source_value)}'", object_path, ) @@ -166,14 +164,12 @@ class UnitBase(IntBase): raise SchemaException(f"{type(self._value)} Failed to convert: {self}", object_path) elif isinstance(source_value, int): raise SchemaException( - "We do not accept number without units." - f" Please convert the value to string an add a unit - {list(type(self)._units.keys())}", + f"number without units, please convert to string and add unit - {list(type(self)._units.keys())}", object_path, ) else: raise SchemaException( - f"Unexpected input type for Unit type - {type(source_value)}." - " Cause might be invalid format or invalid type.", + f"expected number with units in a string, got '{type(source_value)}'.", object_path, ) diff --git a/manager/knot_resolver_manager/exceptions.py b/manager/knot_resolver_manager/exceptions.py index 3b0486e68..c899dba93 100644 --- a/manager/knot_resolver_manager/exceptions.py +++ b/manager/knot_resolver_manager/exceptions.py @@ -10,6 +10,7 @@ class KresManagerException(Exception): class SubprocessControllerException(KresManagerException): pass + class SubprocessControllerTimeoutException(KresManagerException): pass @@ -24,7 +25,7 @@ class SchemaException(KresManagerException): return self._tree_path def msg(self): - return f"field {self.where()}: " + super().__str__() + return f"[{self.where()}] " + super().__str__() def recursive_msg(self, indentation_level: int = 0) -> str: INDENT = indentation_level * "\t" diff --git a/manager/knot_resolver_manager/kresd_controller/systemd/dbus_api.py b/manager/knot_resolver_manager/kresd_controller/systemd/dbus_api.py index 9343b8c25..e087f1de4 100644 --- a/manager/knot_resolver_manager/kresd_controller/systemd/dbus_api.py +++ b/manager/knot_resolver_manager/kresd_controller/systemd/dbus_api.py @@ -15,8 +15,8 @@ from typing_extensions import Literal from knot_resolver_manager.compat.dataclasses import dataclass from knot_resolver_manager.constants import kres_gc_executable, kresd_cache_dir, kresd_config_file, kresd_executable from knot_resolver_manager.datamodel.config_schema import KresConfig -from knot_resolver_manager.kresd_controller.interface import KresID, SubprocessType from knot_resolver_manager.exceptions import SubprocessControllerException, SubprocessControllerTimeoutException +from knot_resolver_manager.kresd_controller.interface import KresID, SubprocessType logger = logging.getLogger(__name__) @@ -118,7 +118,9 @@ def _wait_for_job_completion(systemd: Any, job_creating_func: Callable[[], str], if result_state == "timeout": raise SubprocessControllerTimeoutException(f"systemd job '{job_path}' did not finish in {timeout_sec} seconds") if result_state != "done": - raise SubprocessControllerException(f"systemd job '{job_path}' completed with state '{result_state}' instead of expected 'done'") + raise SubprocessControllerException( + f"systemd job '{job_path}' completed with state '{result_state}' instead of expected 'done'" + ) @_wrap_dbus_errors @@ -248,8 +250,7 @@ def start_transient_kresd_unit(config: KresConfig, type_: SystemdType, kres_id: _wait_for_job_completion(systemd, job) except SubprocessControllerTimeoutException: logger.error( - f"Failed to start transient '{name}'." - "The start operation did not finish within the expected timeframe" + f"Failed to start transient '{name}'." "The start operation did not finish within the expected timeframe" ) raise except SubprocessControllerException as e: diff --git a/manager/knot_resolver_manager/utils/modelling.py b/manager/knot_resolver_manager/utils/modelling.py index 897d6aaa4..4ca2f2641 100644 --- a/manager/knot_resolver_manager/utils/modelling.py +++ b/manager/knot_resolver_manager/utils/modelling.py @@ -273,7 +273,7 @@ def _validated_object_type( except SchemaException as e: errs.append(e) - raise SchemaException("failed to parse union type, all variants failed", object_path, child_exceptions=errs) + raise SchemaException("could not parse any of the possible variants", object_path, child_exceptions=errs) # after this, there is no place for a None object elif obj is None: @@ -339,6 +339,8 @@ def _validated_object_type( # List[T] elif is_list(cls): + if isinstance(obj, str): + raise SchemaException("expected list, got string", object_path) return _validated_list(cls, obj, object_path) # Tuple[A,B,C,D,...] -- 2.47.3