catch DB errors in sync

This commit is contained in:
Damien Elmes 2020-02-04 15:16:11 +10:00
parent d7e4d10184
commit c329759a88

View File

@ -15,6 +15,7 @@ from anki.lang import _
from anki.media import media_paths_from_col_path from anki.media import media_paths_from_col_path
from anki.rsbackend import ( from anki.rsbackend import (
AnkiWebAuthFailed, AnkiWebAuthFailed,
DBError,
Interrupted, Interrupted,
MediaSyncDownloadedChanges, MediaSyncDownloadedChanges,
MediaSyncDownloadedFiles, MediaSyncDownloadedFiles,
@ -117,7 +118,9 @@ class MediaSyncer:
(media_folder, media_db) = media_paths_from_col_path(self.mw.col.path) (media_folder, media_db) = media_paths_from_col_path(self.mw.col.path)
def run() -> None: def run() -> None:
self.mw.col.backend.sync_media(hkey, media_folder, media_db, self._endpoint()) self.mw.col.backend.sync_media(
hkey, media_folder, media_db, self._endpoint()
)
self.mw.taskman.run_in_background(run, self._on_finished) self.mw.taskman.run_in_background(run, self._on_finished)
@ -147,19 +150,22 @@ class MediaSyncer:
self._log_and_notify(_("Media sync complete.")) self._log_and_notify(_("Media sync complete."))
def _handle_sync_error(self, exc: BaseException): def _handle_sync_error(self, exc: BaseException):
if isinstance(exc, Interrupted):
self._log_and_notify(_("Media sync aborted."))
return
self._log_and_notify(_("Media sync failed."))
if isinstance(exc, AnkiWebAuthFailed): if isinstance(exc, AnkiWebAuthFailed):
self.mw.pm.set_sync_key(None) self.mw.pm.set_sync_key(None)
self._log_and_notify(_("Authentication failed."))
showWarning(_("AnkiWeb ID or password was incorrect; please try again.")) showWarning(_("AnkiWeb ID or password was incorrect; please try again."))
elif isinstance(exc, Interrupted):
self._log_and_notify(_("Media sync aborted."))
elif isinstance(exc, NetworkError): elif isinstance(exc, NetworkError):
self._log_and_notify(_("Network error."))
showWarning( showWarning(
_("Syncing failed; please check your internet connection.") _("Syncing failed; please check your internet connection.")
+ "\n\n" + "\n\n"
+ _("Detailed error: {}").format(str(exc)) + _("Detailed error: {}").format(str(exc))
) )
elif isinstance(exc, DBError):
showWarning(_("Problem accessing the media database: {}").format(str(exc)))
else: else:
raise exc raise exc