Handle more exceptions in azure_storage (#145320)

This commit is contained in:
Josef Zweck 2025-05-20 19:42:10 +02:00 committed by GitHub
parent 5d76d92bcf
commit 37e13505cf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 39 additions and 11 deletions

View File

@ -2,8 +2,8 @@
from aiohttp import ClientTimeout from aiohttp import ClientTimeout
from azure.core.exceptions import ( from azure.core.exceptions import (
AzureError,
ClientAuthenticationError, ClientAuthenticationError,
HttpResponseError,
ResourceNotFoundError, ResourceNotFoundError,
) )
from azure.core.pipeline.transport._aiohttp import ( from azure.core.pipeline.transport._aiohttp import (
@ -70,7 +70,7 @@ async def async_setup_entry(
translation_key="invalid_auth", translation_key="invalid_auth",
translation_placeholders={CONF_ACCOUNT_NAME: entry.data[CONF_ACCOUNT_NAME]}, translation_placeholders={CONF_ACCOUNT_NAME: entry.data[CONF_ACCOUNT_NAME]},
) from err ) from err
except HttpResponseError as err: except AzureError as err:
raise ConfigEntryNotReady( raise ConfigEntryNotReady(
translation_domain=DOMAIN, translation_domain=DOMAIN,
translation_key="cannot_connect", translation_key="cannot_connect",

View File

@ -8,7 +8,7 @@ import json
import logging import logging
from typing import Any, Concatenate from typing import Any, Concatenate
from azure.core.exceptions import HttpResponseError from azure.core.exceptions import AzureError, HttpResponseError, ServiceRequestError
from azure.storage.blob import BlobProperties from azure.storage.blob import BlobProperties
from homeassistant.components.backup import ( from homeassistant.components.backup import (
@ -80,6 +80,20 @@ def handle_backup_errors[_R, **P](
f"Error during backup operation in {func.__name__}:" f"Error during backup operation in {func.__name__}:"
f" Status {err.status_code}, message: {err.message}" f" Status {err.status_code}, message: {err.message}"
) from err ) from err
except ServiceRequestError as err:
raise BackupAgentError(
f"Timeout during backup operation in {func.__name__}"
) from err
except AzureError as err:
_LOGGER.debug(
"Error during backup in %s: %s",
func.__name__,
err,
exc_info=True,
)
raise BackupAgentError(
f"Error during backup operation in {func.__name__}: {err}"
) from err
return wrapper return wrapper

View File

@ -6,7 +6,7 @@ from collections.abc import AsyncGenerator
from io import StringIO from io import StringIO
from unittest.mock import ANY, Mock, patch from unittest.mock import ANY, Mock, patch
from azure.core.exceptions import HttpResponseError from azure.core.exceptions import AzureError, HttpResponseError, ServiceRequestError
from azure.storage.blob import BlobProperties from azure.storage.blob import BlobProperties
import pytest import pytest
@ -276,14 +276,33 @@ async def test_agents_error_on_download_not_found(
assert mock_client.download_blob.call_count == 0 assert mock_client.download_blob.call_count == 0
@pytest.mark.parametrize(
("error", "message"),
[
(
HttpResponseError("http error"),
"Error during backup operation in async_delete_backup: Status None, message: http error",
),
(
ServiceRequestError("timeout"),
"Timeout during backup operation in async_delete_backup",
),
(
AzureError("generic error"),
"Error during backup operation in async_delete_backup: generic error",
),
],
)
async def test_error_during_delete( async def test_error_during_delete(
hass: HomeAssistant, hass: HomeAssistant,
hass_ws_client: WebSocketGenerator, hass_ws_client: WebSocketGenerator,
mock_client: MagicMock, mock_client: MagicMock,
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
error: Exception,
message: str,
) -> None: ) -> None:
"""Test the error wrapper.""" """Test the error wrapper."""
mock_client.delete_blob.side_effect = HttpResponseError("Failed to delete backup") mock_client.delete_blob.side_effect = error
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
@ -297,12 +316,7 @@ async def test_error_during_delete(
assert response["success"] assert response["success"]
assert response["result"] == { assert response["result"] == {
"agent_errors": { "agent_errors": {f"{DOMAIN}.{mock_config_entry.entry_id}": message}
f"{DOMAIN}.{mock_config_entry.entry_id}": (
"Error during backup operation in async_delete_backup: "
"Status None, message: Failed to delete backup"
)
}
} }