From d8b51b4f2c162af1ed36787813f4ec6d3d5ec030 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 8 Oct 2024 15:59:45 +0200 Subject: [PATCH] Avoid unknown error translation strings in anthropic (#127823) --- homeassistant/components/anthropic/config_flow.py | 11 +++++++---- tests/components/anthropic/test_config_flow.py | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/anthropic/config_flow.py b/homeassistant/components/anthropic/config_flow.py index 01e16ec5350..5ea167090c6 100644 --- a/homeassistant/components/anthropic/config_flow.py +++ b/homeassistant/components/anthropic/config_flow.py @@ -87,10 +87,13 @@ class AnthropicConfigFlow(ConfigFlow, domain=DOMAIN): except anthropic.APIConnectionError: errors["base"] = "cannot_connect" except anthropic.APIStatusError as e: - if isinstance(e.body, dict): - errors["base"] = e.body.get("error", {}).get("type", "unknown") - else: - errors["base"] = "unknown" + errors["base"] = "unknown" + if ( + isinstance(e.body, dict) + and (error := e.body.get("error")) + and error.get("type") == "authentication_error" + ): + errors["base"] = "authentication_error" except Exception: _LOGGER.exception("Unexpected exception") errors["base"] = "unknown" diff --git a/tests/components/anthropic/test_config_flow.py b/tests/components/anthropic/test_config_flow.py index df27352b7b2..a5a025b00d0 100644 --- a/tests/components/anthropic/test_config_flow.py +++ b/tests/components/anthropic/test_config_flow.py @@ -108,7 +108,7 @@ async def test_options( ), body={"type": "error", "error": {"type": "invalid_request_error"}}, ), - "invalid_request_error", + "unknown", ), ( AuthenticationError(