Check for errors when creating backups using supervisor (#137220)

* Check for errors when creating backups using supervisor

* Improve error reporting when there's no backup reference
This commit is contained in:
Erik Montnemery 2025-02-03 17:33:03 +01:00 committed by GitHub
parent a41566611e
commit 58b7be7c2f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 9 deletions

View File

@ -354,6 +354,7 @@ class SupervisorBackupReaderWriter(BackupReaderWriter):
"""Wait for a backup to complete."""
backup_complete = asyncio.Event()
backup_id: str | None = None
create_errors: list[dict[str, str]] = []
@callback
def on_job_progress(data: Mapping[str, Any]) -> None:
@ -361,6 +362,7 @@ class SupervisorBackupReaderWriter(BackupReaderWriter):
nonlocal backup_id
if data.get("done") is True:
backup_id = data.get("reference")
create_errors.extend(data.get("errors", []))
backup_complete.set()
unsub = self._async_listen_job_events(backup.job_id, on_job_progress)
@ -369,8 +371,11 @@ class SupervisorBackupReaderWriter(BackupReaderWriter):
await backup_complete.wait()
finally:
unsub()
if not backup_id:
raise BackupReaderWriterError("Backup failed")
if not backup_id or create_errors:
# We should add more specific error handling here in the future
raise BackupReaderWriterError(
f"Backup failed: {create_errors or 'no backup_id'}"
)
async def open_backup() -> AsyncIterator[bytes]:
try:

View File

@ -1360,11 +1360,40 @@ async def test_reader_writer_create_partial_backup_error(
assert supervisor_client.backups.partial_backup.call_count == 1
@pytest.mark.parametrize(
"supervisor_event",
[
# Missing backup reference
{
"event": "job",
"data": {
"done": True,
"uuid": TEST_JOB_ID,
},
},
# Errors
{
"event": "job",
"data": {
"done": True,
"errors": [
{
"type": "BackupMountDownError",
"message": "test_mount is down, cannot back-up to it",
}
],
"uuid": TEST_JOB_ID,
"reference": "test_slug",
},
},
],
)
@pytest.mark.usefixtures("hassio_client", "setup_integration")
async def test_reader_writer_create_missing_reference_error(
hass: HomeAssistant,
hass_ws_client: WebSocketGenerator,
supervisor_client: AsyncMock,
supervisor_event: dict[str, Any],
) -> None:
"""Test missing reference error when generating a backup."""
client = await hass_ws_client(hass)
@ -1395,13 +1424,7 @@ async def test_reader_writer_create_missing_reference_error(
assert supervisor_client.backups.partial_backup.call_count == 1
await client.send_json_auto_id(
{
"type": "supervisor/event",
"data": {
"event": "job",
"data": {"done": True, "uuid": TEST_JOB_ID},
},
}
{"type": "supervisor/event", "data": supervisor_event}
)
response = await client.receive_json()
assert response["success"]