From: Aleš Mrázek Date: Fri, 12 Dec 2025 00:42:18 +0000 (+0100) Subject: */exceptions.py: updated for more strict linter (Ruff) X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=28194b138d57cbe1bbb0251141e4ec98595f1c64;p=thirdparty%2Fknot-resolver.git */exceptions.py: updated for more strict linter (Ruff) --- diff --git a/python/knot_resolver/controller/exceptions.py b/python/knot_resolver/controller/exceptions.py index 99fdad3ce..ceb6145f4 100644 --- a/python/knot_resolver/controller/exceptions.py +++ b/python/knot_resolver/controller/exceptions.py @@ -1,17 +1,19 @@ from typing import List -from knot_resolver import KresBaseException +from knot_resolver import KresBaseError -class SubprocessControllerError(KresBaseException): - pass +class KresSubprocessControllerError(KresBaseError): + """Class for errors that are raised in the controller module.""" -class SubprocessControllerExecError(Exception): +class KresSubprocessControllerExec(Exception): # noqa: N818 """ - Exception that is used to deliberately terminate system startup - and make exec() of something else. This is used by the subprocess controller - as supervisord to run as the top-level process in a process tree hierarchy. + Custom non-error exception that indicates the need for exec(). + + Raised by the controller (supervisord) and caught by the controlled process (manager). + The exception says that the process needs to perform a re-exec during startup. + This ensures that the process runs under the controller (supervisord) in a process tree hierarchy. """ def __init__(self, exec_args: List[str], *args: object) -> None: diff --git a/python/knot_resolver/manager/exceptions.py b/python/knot_resolver/manager/exceptions.py index 77bc4d9f6..f4be5e8b8 100644 --- a/python/knot_resolver/manager/exceptions.py +++ b/python/knot_resolver/manager/exceptions.py @@ -1,5 +1,5 @@ -from knot_resolver import KresBaseException +from knot_resolver import KresBaseError -class KresManagerException(KresBaseException): - pass +class KresManagerBaseError(KresBaseError): + """Base class for all errors used in the manager module.""" diff --git a/python/knot_resolver/utils/modeling/exceptions.py b/python/knot_resolver/utils/modeling/exceptions.py index ef6df3578..10660b510 100644 --- a/python/knot_resolver/utils/modeling/exceptions.py +++ b/python/knot_resolver/utils/modeling/exceptions.py @@ -1,24 +1,24 @@ -from typing import Iterable, List +from typing import Iterable, Iterator -from knot_resolver import KresBaseException +from knot_resolver import KresBaseError -class DataModelingBaseException(KresBaseException): - """ - Base class for all exceptions used in modelling. - """ +class ModelingBaseError(KresBaseError): + """Base class for all errors used in data modeling.""" -class DataParsingError(DataModelingBaseException): - pass +class DataDescriptionError(ModelingBaseError): + """Class for errors that are raised when checking data description.""" -class DataDescriptionError(DataModelingBaseException): - pass +class DataParsingError(ModelingBaseError): + """Class for errors that are raised when parsing data.""" -class DataValidationError(DataModelingBaseException): - def __init__(self, msg: str, tree_path: str, child_exceptions: "Iterable[DataValidationError]" = tuple()) -> None: +class DataValidationError(ModelingBaseError): + """Class for errors that are raised when validating data.""" + + def __init__(self, msg: str, tree_path: str, child_exceptions: Iterable["DataValidationError"] = ()) -> None: super().__init__(msg) self._tree_path = tree_path.replace("_", "-") self._child_exceptions = child_exceptions @@ -26,38 +26,41 @@ class DataValidationError(DataModelingBaseException): def where(self) -> str: return self._tree_path - def msg(self): + def msg(self) -> str: return f"[{self.where()}] {super().__str__()}" def recursive_msg(self, indentation_level: int = 0) -> str: - msg_parts: List[str] = [] + def indented_lines(level: int) -> Iterator[str]: + if level == 0: + yield "Configuration validation error detected:" + level += 1 - if indentation_level == 0: - indentation_level += 1 - msg_parts.append("Configuration validation error detected:") + indent = "\t" * level + yield f"{indent}{self.msg()}" - indent = indentation_level * "\t" - msg_parts.append(f"{indent}{self.msg()}") + for child in self._child_exceptions: + yield from child.recursive_msg(level + 1).split("\n") - for c in self._child_exceptions: - msg_parts.append(c.recursive_msg(indentation_level + 1)) - return "\n".join(msg_parts) + return "\n".join(indented_lines(indentation_level)) def __str__(self) -> str: return self.recursive_msg() class AggregateDataValidationError(DataValidationError): - def __init__(self, object_path: str, child_exceptions: "Iterable[DataValidationError]") -> None: + """Aggregation class for errors (DataValidationError) raised during data validation.""" + + def __init__(self, object_path: str, child_exceptions: Iterable[DataValidationError]) -> None: super().__init__("error due to lower level exceptions", object_path, child_exceptions) def recursive_msg(self, indentation_level: int = 0) -> str: - inc = 0 - msg_parts: List[str] = [] - if indentation_level == 0: - inc = 1 - msg_parts.append("Configuration validation errors detected:") - - for c in self._child_exceptions: - msg_parts.append(c.recursive_msg(indentation_level + inc)) - return "\n".join(msg_parts) + def indented_lines(level: int) -> Iterator[str]: + inc = 0 + if level == 0: + yield "Configuration validation errors detected:" + inc = 1 + + for child in self._child_exceptions: + yield from child.recursive_msg(level + inc).split("\n") + + return "\n".join(indented_lines(indentation_level))