diff --git a/src/ahriman/core/status/web_client.py b/src/ahriman/core/status/web_client.py index da121118..176a323e 100644 --- a/src/ahriman/core/status/web_client.py +++ b/src/ahriman/core/status/web_client.py @@ -40,6 +40,7 @@ class WebClient(Client, LazyLogging): Attributes: address(str): address of the web service + suppress_errors(bool): suppress logging errors (e.g. if no web server available) user(User | None): web service user descriptor """ @@ -54,6 +55,7 @@ class WebClient(Client, LazyLogging): self.user = User.from_option( configuration.get("web", "username", fallback=None), configuration.get("web", "password", fallback=None)) + self.suppress_errors = configuration.getboolean("settings", "suppress_http_log_errors", fallback=False) self.__session = self._create_session(use_unix_socket=use_unix_socket) @@ -106,9 +108,13 @@ class WebClient(Client, LazyLogging): """ try: yield - except requests.HTTPError as e: + except requests.RequestException as e: + if self.suppress_errors: + return self.logger.exception("could not perform http request: %s", exception_response_text(e)) except Exception: + if self.suppress_errors: + return self.logger.exception("could not perform http request") def _create_session(self, *, use_unix_socket: bool) -> requests.Session: diff --git a/src/ahriman/core/util.py b/src/ahriman/core/util.py index 357bc8b8..239a5755 100644 --- a/src/ahriman/core/util.py +++ b/src/ahriman/core/util.py @@ -175,12 +175,12 @@ def enum_values(enum: type[Enum]) -> list[str]: return [str(key.value) for key in enum] # explicit str conversion for typing -def exception_response_text(exception: requests.exceptions.HTTPError) -> str: +def exception_response_text(exception: requests.exceptions.RequestException) -> str: """ safe response exception text generation Args: - exception(requests.exceptions.HTTPError): exception raised + exception(requests.exceptions.RequestException): exception raised Returns: str: text of the response if it is not None and empty string otherwise diff --git a/tests/ahriman/core/status/test_web_client.py b/tests/ahriman/core/status/test_web_client.py index a9016332..a259172d 100644 --- a/tests/ahriman/core/status/test_web_client.py +++ b/tests/ahriman/core/status/test_web_client.py @@ -157,6 +157,30 @@ def test_add_failed_http_error(web_client: WebClient, package_ahriman: Package, web_client.add(package_ahriman, BuildStatusEnum.Unknown) +def test_add_failed_suppress(web_client: WebClient, package_ahriman: Package, mocker: MockerFixture) -> None: + """ + must suppress any exception happened during addition and don't log + """ + web_client.suppress_errors = True + mocker.patch("requests.Session.post", side_effect=Exception()) + logging_mock = mocker.patch("logging.exception") + + web_client.add(package_ahriman, BuildStatusEnum.Unknown) + logging_mock.assert_not_called() + + +def test_add_failed_http_error_suppress(web_client: WebClient, package_ahriman: Package, mocker: MockerFixture) -> None: + """ + must suppress HTTP exception happened during addition and don't log + """ + web_client.suppress_errors = True + mocker.patch("requests.Session.post", side_effect=requests.exceptions.HTTPError()) + logging_mock = mocker.patch("logging.exception") + + web_client.add(package_ahriman, BuildStatusEnum.Unknown) + logging_mock.assert_not_called() + + def test_get_all(web_client: WebClient, package_ahriman: Package, mocker: MockerFixture) -> None: """ must return all packages status