Skip to content

Commit

Permalink
Simplify error handling when creating backup (#134528)
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery authored and frenck committed Jan 3, 2025
1 parent c574629 commit 7ea7178
Showing 1 changed file with 12 additions and 24 deletions.
36 changes: 12 additions & 24 deletions homeassistant/components/backup/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,12 +800,10 @@ async def _async_finish_backup(
"""Finish a backup."""
if TYPE_CHECKING:
assert self._backup_task is not None
backup_success = False
try:
written_backup = await self._backup_task
except Exception as err:
self.async_on_backup_event(
CreateBackupEvent(stage=None, state=CreateBackupState.FAILED)
)
if with_automatic_settings:
self._update_issue_backup_failed()

Expand All @@ -831,33 +829,15 @@ async def _async_finish_backup(
agent_ids=agent_ids,
open_stream=written_backup.open_stream,
)
except BaseException:
self.async_on_backup_event(
CreateBackupEvent(stage=None, state=CreateBackupState.FAILED)
)
raise # manager or unexpected error
finally:
try:
await written_backup.release_stream()
except Exception:
self.async_on_backup_event(
CreateBackupEvent(stage=None, state=CreateBackupState.FAILED)
)
raise
await written_backup.release_stream()
self.known_backups.add(written_backup.backup, agent_errors)
if agent_errors:
self.async_on_backup_event(
CreateBackupEvent(stage=None, state=CreateBackupState.FAILED)
)
else:
if not agent_errors:
if with_automatic_settings:
# create backup was successful, update last_completed_automatic_backup
self.config.data.last_completed_automatic_backup = dt_util.now()
self.store.save()

self.async_on_backup_event(
CreateBackupEvent(stage=None, state=CreateBackupState.COMPLETED)
)
backup_success = True

if with_automatic_settings:
self._update_issue_after_agent_upload(agent_errors)
Expand All @@ -868,6 +848,14 @@ async def _async_finish_backup(
finally:
self._backup_task = None
self._backup_finish_task = None
self.async_on_backup_event(
CreateBackupEvent(
stage=None,
state=CreateBackupState.COMPLETED
if backup_success
else CreateBackupState.FAILED,
)
)
self.async_on_backup_event(IdleEvent())

async def async_restore_backup(
Expand Down

0 comments on commit 7ea7178

Please # to comment.