raise BackupAgentUnreachableError when NAS is unavailable

This commit is contained in:
mib1185
2025-03-16 13:24:29 +00:00
parent 77f8ddd948
commit 4d8cfae396
3 changed files with 32 additions and 8 deletions

View File

@@ -14,6 +14,7 @@ from .agent import (
BackupAgent, BackupAgent,
BackupAgentError, BackupAgentError,
BackupAgentPlatformProtocol, BackupAgentPlatformProtocol,
BackupAgentUnreachableError,
LocalBackupAgent, LocalBackupAgent,
) )
from .config import BackupConfig, CreateBackupParametersDict from .config import BackupConfig, CreateBackupParametersDict
@@ -49,6 +50,7 @@ __all__ = [
"BackupAgent", "BackupAgent",
"BackupAgentError", "BackupAgentError",
"BackupAgentPlatformProtocol", "BackupAgentPlatformProtocol",
"BackupAgentUnreachableError",
"BackupConfig", "BackupConfig",
"BackupManagerError", "BackupManagerError",
"BackupNotFound", "BackupNotFound",

View File

@@ -8,12 +8,16 @@ from typing import TYPE_CHECKING, Any
from aiohttp import StreamReader from aiohttp import StreamReader
from synology_dsm.api.file_station import SynoFileStation from synology_dsm.api.file_station import SynoFileStation
from synology_dsm.exceptions import SynologyDSMAPIErrorException from synology_dsm.exceptions import (
SynologyDSMAPIErrorException,
SynologyDSMRequestException,
)
from homeassistant.components.backup import ( from homeassistant.components.backup import (
AgentBackup, AgentBackup,
BackupAgent, BackupAgent,
BackupAgentError, BackupAgentError,
BackupAgentUnreachableError,
BackupNotFound, BackupNotFound,
suggested_filename, suggested_filename,
) )
@@ -238,6 +242,8 @@ class SynologyDSMBackupAgent(BackupAgent):
files = await self._file_station.get_files(path=self.path) files = await self._file_station.get_files(path=self.path)
except SynologyDSMAPIErrorException as err: except SynologyDSMAPIErrorException as err:
raise BackupAgentError("Failed to list backups") from err raise BackupAgentError("Failed to list backups") from err
except SynologyDSMRequestException as err:
raise BackupAgentUnreachableError from err
if TYPE_CHECKING: if TYPE_CHECKING:
assert files assert files

View File

@@ -4,9 +4,14 @@ from io import StringIO
from typing import Any from typing import Any
from unittest.mock import ANY, AsyncMock, MagicMock, Mock, patch from unittest.mock import ANY, AsyncMock, MagicMock, Mock, patch
from aiohttp import ClientError
import pytest import pytest
from synology_dsm.api.file_station.models import SynoFileFile, SynoFileSharedFolder from synology_dsm.api.file_station.models import SynoFileFile, SynoFileSharedFolder
from synology_dsm.exceptions import SynologyDSMAPIErrorException from synology_dsm.exceptions import (
SynologyDSMAPIErrorException,
SynologyDSMException,
SynologyDSMRequestException,
)
from homeassistant.components.backup import ( from homeassistant.components.backup import (
DOMAIN as BACKUP_DOMAIN, DOMAIN as BACKUP_DOMAIN,
@@ -315,26 +320,37 @@ async def test_agents_list_backups(
] ]
@pytest.mark.parametrize(
("error", "expected_aget_errors"),
[
(
SynologyDSMAPIErrorException("api", "500", "error"),
{"synology_dsm.mocked_syno_dsm_entry": "Failed to list backups"},
),
(
SynologyDSMRequestException(ClientError("error")),
{"synology_dsm.mocked_syno_dsm_entry": "The backup agent is unreachable."},
),
],
)
async def test_agents_list_backups_error( async def test_agents_list_backups_error(
hass: HomeAssistant, hass: HomeAssistant,
setup_dsm_with_filestation: MagicMock, setup_dsm_with_filestation: MagicMock,
hass_ws_client: WebSocketGenerator, hass_ws_client: WebSocketGenerator,
error: SynologyDSMException,
expected_aget_errors: dict[str, str],
) -> None: ) -> None:
"""Test agent error while list backups.""" """Test agent error while list backups."""
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
setup_dsm_with_filestation.file.get_files.side_effect = ( setup_dsm_with_filestation.file.get_files.side_effect = error
SynologyDSMAPIErrorException("api", "500", "error")
)
await client.send_json_auto_id({"type": "backup/info"}) await client.send_json_auto_id({"type": "backup/info"})
response = await client.receive_json() response = await client.receive_json()
assert response["success"] assert response["success"]
assert response["result"] == { assert response["result"] == {
"agent_errors": { "agent_errors": expected_aget_errors,
"synology_dsm.mocked_syno_dsm_entry": "Failed to list backups"
},
"backups": [], "backups": [],
"last_attempted_automatic_backup": None, "last_attempted_automatic_backup": None,
"last_completed_automatic_backup": None, "last_completed_automatic_backup": None,