Handle uncaught exceptions in Analytics insights (#117558)

pull/117567/head
Joost Lekkerkerker 2024-05-16 13:43:03 +02:00 committed by GitHub
parent 59645aeb0f
commit 4cded378bf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 11 deletions

View File

@ -82,6 +82,9 @@ class HomeassistantAnalyticsConfigFlow(ConfigFlow, domain=DOMAIN):
except HomeassistantAnalyticsConnectionError:
LOGGER.exception("Error connecting to Home Assistant analytics")
return self.async_abort(reason="cannot_connect")
except Exception: # noqa: BLE001
LOGGER.exception("Unexpected error")
return self.async_abort(reason="unknown")
options = [
SelectOptionDict(

View File

@ -13,7 +13,8 @@
}
},
"abort": {
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]"
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
"unknown": "[%key:common::config_flow::error::unknown%]"
},
"error": {
"no_integration_selected": "You must select at least one integration to track"

View File

@ -6,12 +6,12 @@ from unittest.mock import AsyncMock
import pytest
from python_homeassistant_analytics import HomeassistantAnalyticsConnectionError
from homeassistant import config_entries
from homeassistant.components.analytics_insights.const import (
CONF_TRACKED_CUSTOM_INTEGRATIONS,
CONF_TRACKED_INTEGRATIONS,
DOMAIN,
)
from homeassistant.config_entries import SOURCE_USER
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
@ -61,7 +61,7 @@ async def test_form(
) -> None:
"""Test we get the form."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
@ -96,7 +96,7 @@ async def test_submitting_empty_form(
) -> None:
"""Test we can't submit an empty form."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
@ -128,20 +128,28 @@ async def test_submitting_empty_form(
assert len(mock_setup_entry.mock_calls) == 1
@pytest.mark.parametrize(
("exception", "reason"),
[
(HomeassistantAnalyticsConnectionError, "cannot_connect"),
(Exception, "unknown"),
],
)
async def test_form_cannot_connect(
hass: HomeAssistant, mock_analytics_client: AsyncMock
hass: HomeAssistant,
mock_analytics_client: AsyncMock,
exception: Exception,
reason: str,
) -> None:
"""Test we handle cannot connect error."""
mock_analytics_client.get_integrations.side_effect = (
HomeassistantAnalyticsConnectionError
)
mock_analytics_client.get_integrations.side_effect = exception
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "cannot_connect"
assert result["reason"] == reason
async def test_form_already_configured(
@ -159,7 +167,7 @@ async def test_form_already_configured(
entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "single_instance_allowed"