From 41636f41f8d8a1e3fe1452e73870e133e404edcb Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Mon, 5 Dec 2011 17:40:13 +0900 Subject: [PATCH] tweak sync error handling --- anki/consts.py | 4 +--- anki/sync.py | 33 ++++++++++++++++----------------- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/anki/consts.py b/anki/consts.py index feae484a0..b215ed3e8 100644 --- a/anki/consts.py +++ b/anki/consts.py @@ -39,9 +39,7 @@ MEDIA_REM = 1 SYNC_ZIP_SIZE = 10*1024*1024 CHUNK_SIZE = 65536 MIME_BOUNDARY = "Anki-sync-boundary" -SYNC_HOST = os.environ.get("SYNC_HOST") or "dev.ankiweb.net" -SYNC_PORT = int(os.environ.get("SYNC_PORT") or 80) -SYNC_URL = "http://%s:%d/sync/" % (SYNC_HOST, SYNC_PORT) +SYNC_URL = os.environ.get("SYNC_URL") or "https://ankiweb.net/sync/" SYNC_VER = 0 HTTP_CERTS = os.path.join(os.path.dirname(__file__), "ankiweb.certs") HTTP_TIMEOUT = 60 diff --git a/anki/sync.py b/anki/sync.py index 23a843cca..795fcb514 100644 --- a/anki/sync.py +++ b/anki/sync.py @@ -382,11 +382,20 @@ class LocalServer(Syncer): # HTTP syncing tools ########################################################################## +# Calling code should catch the following codes: +# - 501: client needs upgrade +# - 502: ankiweb down +# - 503/504: server too busy + class HttpSyncer(object): def _vars(self): return dict(k=self.hkey) + def assertOk(self, resp): + if resp['status'] != '200': + raise Exception("Unknown response code: %s" % resp['status']) + # Posting data as a file ###################################################################### # We don't want to post the payload as a form var, as the percent-encoding is @@ -433,8 +442,7 @@ Content-Type: application/octet-stream\r\n\r\n""") buf.close() resp, cont = http.request( SYNC_URL+method, "POST", headers=headers, body=body) - if resp['status'] != '200': - raise Exception("Invalid response code: %s" % resp['status']) + self.assertOk(resp) return cont # Incremental sync over HTTP @@ -452,15 +460,12 @@ class RemoteServer(Syncer, HttpSyncer): pw = pw.encode("utf-8") resp, cont = self.con.request( SYNC_URL+"hostKey?" + urllib.urlencode(dict(u=user,p=pw))) - if resp['status'] == '200': - self.hkey = simplejson.loads(cont)['key'] - return self.hkey - elif resp['status'] == '403': + if resp['status'] == '403': # invalid auth return - else: - raise Exception("Unknown response code: %s" % resp['status']) - return + self.assertOk(resp) + self.hkey = simplejson.loads(cont)['key'] + return self.hkey def meta(self): resp, cont = self.con.request( @@ -468,12 +473,7 @@ class RemoteServer(Syncer, HttpSyncer): if resp['status'] == '403': # auth failure return - elif resp['status'] in ('503', '504'): - raise Exception("Server is too busy; please try again later.") - elif resp['status'] == '501': - raise Exception("Your client is out of date; please upgrade.") - elif resp['status'] != '200': - raise Exception("Unknown response code: %s" % resp['status']) + self.assertOk(resp) return simplejson.loads(cont) def applyChanges(self, **kw): @@ -511,8 +511,7 @@ class FullSyncer(HttpSyncer): self.col.close() resp, cont = self.con.request( SYNC_URL+"download?" + urllib.urlencode(self._vars())) - if resp['status'] != '200': - raise Exception("Invalid response code: %s" % resp['status']) + self.assertOk(resp) tpath = self.col.path + ".tmp" open(tpath, "wb").write(cont) # check the received file is ok