]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
manager: improved formatting of errors
authorVasek Sraier <git@vakabus.cz>
Fri, 11 Mar 2022 11:28:24 +0000 (12:28 +0100)
committerAleš Mrázek <ales.mrazek@nic.cz>
Fri, 8 Apr 2022 14:17:54 +0000 (16:17 +0200)
manager/knot_resolver_manager/datamodel/types/base_types.py
manager/knot_resolver_manager/exceptions.py
manager/knot_resolver_manager/kresd_controller/systemd/dbus_api.py
manager/knot_resolver_manager/utils/modelling.py

index b168fbc44feef4a197a243397203ae6e56a6f9b2..7dc203a7e9a4308416da882eb0721f6f9b28c836 100644 (file)
@@ -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,
             )
 
index 3b0486e68e88e645d5a0af249d22282de177fb8e..c899dba93fe61be72383d22c1dba62ceb76ebaaa 100644 (file)
@@ -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"
index 9343b8c2530e232ca138303d77b2cf62a57d2920..e087f1de410670eb7bbb2d09c7d713c76f76de63 100644 (file)
@@ -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:
index 897d6aaa422e75b1ca3d7fa80eed82936f078c54..4ca2f26418ba19ab2cd440e6675c5edbfa1dd9a2 100644 (file)
@@ -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,...]