tweak sync error handling

This commit is contained in:
Damien Elmes 2011-12-05 17:40:13 +09:00
parent 0395047579
commit 41636f41f8
2 changed files with 17 additions and 20 deletions

View file

@ -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

View file

@ -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