From 2a622a929d48c1b82c9822d738d718450964ca0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20=C4=8Cerm=C3=A1k?= Date: Thu, 25 Apr 2024 10:44:21 +0200 Subject: [PATCH] Limit reporting of errors in Supervisor logs fallback (#5040) We do not need to capture HostNotSupported errors to Sentry. The only possible code path this error might come from is where the Journal Gateway Daemon socket is unavailable, which is already reported as an "Unsupported system" repair. --- supervisor/api/__init__.py | 7 +++++-- tests/api/test_supervisor.py | 28 +++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/supervisor/api/__init__.py b/supervisor/api/__init__.py index cf56a354f..e91c0383a 100644 --- a/supervisor/api/__init__.py +++ b/supervisor/api/__init__.py @@ -9,7 +9,7 @@ from aiohttp_fast_url_dispatcher import FastUrlDispatcher, attach_fast_url_dispa from ..const import AddonState from ..coresys import CoreSys, CoreSysAttributes -from ..exceptions import APIAddonNotInstalled +from ..exceptions import APIAddonNotInstalled, HostNotSupportedError from ..utils.sentry import capture_exception from .addons import APIAddons from .audio import APIAudio @@ -410,7 +410,10 @@ class RestAPI(CoreSysAttributes): _LOGGER.exception( "Failed to get supervisor logs using advanced_logs API" ) - capture_exception(err) + if not isinstance(err, HostNotSupportedError): + # No need to capture HostNotSupportedError to Sentry, the cause + # is known and reported to the user using the resolution center. + capture_exception(err) return await api_supervisor.logs(*args, **kwargs) self.webapp.add_routes( diff --git a/tests/api/test_supervisor.py b/tests/api/test_supervisor.py index 86f69067f..483bbd486 100644 --- a/tests/api/test_supervisor.py +++ b/tests/api/test_supervisor.py @@ -6,7 +6,12 @@ from aiohttp.test_utils import TestClient import pytest from supervisor.coresys import CoreSys -from supervisor.exceptions import HassioError, StoreGitError, StoreNotFound +from supervisor.exceptions import ( + HassioError, + HostNotSupportedError, + StoreGitError, + StoreNotFound, +) from supervisor.store.repository import Repository from tests.api import common_test_api_advanced_logs @@ -190,6 +195,27 @@ async def test_api_supervisor_fallback( assert resp.content_type == "text/plain" +async def test_api_supervisor_fallback_log_capture( + api_client: TestClient, journald_logs: MagicMock, docker_logs: MagicMock +): + """Check that Sentry log capture is executed only for unexpected errors.""" + journald_logs.side_effect = HostNotSupportedError( + "No systemd-journal-gatewayd Unix socket available!" + ) + + with patch("supervisor.api.capture_exception") as capture_exception: + await api_client.get("/supervisor/logs") + capture_exception.assert_not_called() + + journald_logs.reset_mock() + + journald_logs.side_effect = HassioError("Something bad happened!") + + with patch("supervisor.api.capture_exception") as capture_exception: + await api_client.get("/supervisor/logs") + capture_exception.assert_called_once() + + async def test_api_supervisor_reload(api_client: TestClient): """Test supervisor reload.""" resp = await api_client.post("/supervisor/reload")