Skip to content

Commit

Permalink
Handle uncaught exceptions in Analytics insights (#117558)
Browse files Browse the repository at this point in the history
  • Loading branch information
joostlek authored and frenck committed May 17, 2024
1 parent 4548ff6 commit ab9ed0e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
3 changes: 3 additions & 0 deletions homeassistant/components/analytics_insights/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ async def async_step_user(
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(
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/analytics_insights/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
28 changes: 18 additions & 10 deletions tests/components/analytics_insights/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand All @@ -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"
Expand Down

0 comments on commit ab9ed0e

Please sign in to comment.