unify sync error handling; handle interrupted

This commit is contained in:
Damien Elmes 2020-05-31 11:46:40 +10:00
parent a8ad4abf37
commit 27a36482c0
2 changed files with 26 additions and 13 deletions

View file

@ -627,6 +627,11 @@ create table if not exists profiles
return None return None
return SyncAuth(hkey=hkey, host_number=self.profile.get("hostNum", 0)) return SyncAuth(hkey=hkey, host_number=self.profile.get("hostNum", 0))
def clear_sync_auth(self) -> None:
self.profile["syncKey"] = None
self.profile["syncUser"] = None
self.profile["hostNum"] = 0
###################################################################### ######################################################################
def apply_profile_options(self) -> None: def apply_profile_options(self) -> None:

View file

@ -10,6 +10,7 @@ import aqt
from anki.rsbackend import ( from anki.rsbackend import (
TR, TR,
FullSyncProgress, FullSyncProgress,
Interrupted,
ProgressKind, ProgressKind,
SyncError, SyncError,
SyncErrorKind, SyncErrorKind,
@ -52,20 +53,26 @@ def get_sync_status(mw: aqt.main.AnkiQt, callback: Callable[[SyncOutput], None])
) )
def handle_sync_error(mw: aqt.main.AnkiQt, err: Exception):
if isinstance(err, SyncError):
if err.kind == SyncErrorKind.AUTH_FAILED:
mw.pm.clear_sync_auth()
elif isinstance(err, Interrupted):
# no message to show
return
showWarning(str(err))
def sync_collection(mw: aqt.main.AnkiQt, on_done: Callable[[], None]) -> None: def sync_collection(mw: aqt.main.AnkiQt, on_done: Callable[[], None]) -> None:
auth = mw.pm.sync_auth() auth = mw.pm.sync_auth()
if not auth: assert auth
sync_login(mw, on_success=lambda: sync_collection(mw))
return
def on_future_done(fut): def on_future_done(fut):
mw.col.db.begin() mw.col.db.begin()
try: try:
out: SyncOutput = fut.result() out: SyncOutput = fut.result()
except InterruptedError: except Exception as err:
return on_done() handle_sync_error(mw, err)
except Exception as e:
showWarning(str(e))
return on_done() return on_done()
mw.pm.set_host_number(out.host_number) mw.pm.set_host_number(out.host_number)
@ -146,8 +153,8 @@ def full_download(mw: aqt.main.AnkiQt, on_done: Callable[[], None]) -> None:
mw.reset() mw.reset()
try: try:
fut.result() fut.result()
except Exception as e: except Exception as err:
showWarning(str(e)) handle_sync_error(mw, err)
return on_done() return on_done()
mw.taskman.with_progress( mw.taskman.with_progress(
@ -173,8 +180,9 @@ def full_upload(mw: aqt.main.AnkiQt, on_done: Callable[[], None]) -> None:
mw.reset() mw.reset()
try: try:
fut.result() fut.result()
except Exception as e: except Exception as err:
showWarning(str(e)) handle_sync_error(mw, err)
return on_done()
return on_done() return on_done()
mw.taskman.with_progress( mw.taskman.with_progress(
@ -202,8 +210,8 @@ def sync_login(
showWarning(str(e)) showWarning(str(e))
sync_login(mw, on_success, username, password) sync_login(mw, on_success, username, password)
return return
except Exception as e: except Exception as err:
showWarning(str(e)) handle_sync_error(mw, err)
return return
mw.pm.set_host_number(auth.host_number) mw.pm.set_host_number(auth.host_number)