mirror of
https://github.com/ankitects/anki.git
synced 2025-09-20 23:12:21 -04:00
tweak sync error handling
This commit is contained in:
parent
0395047579
commit
41636f41f8
2 changed files with 17 additions and 20 deletions
|
@ -39,9 +39,7 @@ MEDIA_REM = 1
|
||||||
SYNC_ZIP_SIZE = 10*1024*1024
|
SYNC_ZIP_SIZE = 10*1024*1024
|
||||||
CHUNK_SIZE = 65536
|
CHUNK_SIZE = 65536
|
||||||
MIME_BOUNDARY = "Anki-sync-boundary"
|
MIME_BOUNDARY = "Anki-sync-boundary"
|
||||||
SYNC_HOST = os.environ.get("SYNC_HOST") or "dev.ankiweb.net"
|
SYNC_URL = os.environ.get("SYNC_URL") or "https://ankiweb.net/sync/"
|
||||||
SYNC_PORT = int(os.environ.get("SYNC_PORT") or 80)
|
|
||||||
SYNC_URL = "http://%s:%d/sync/" % (SYNC_HOST, SYNC_PORT)
|
|
||||||
SYNC_VER = 0
|
SYNC_VER = 0
|
||||||
HTTP_CERTS = os.path.join(os.path.dirname(__file__), "ankiweb.certs")
|
HTTP_CERTS = os.path.join(os.path.dirname(__file__), "ankiweb.certs")
|
||||||
HTTP_TIMEOUT = 60
|
HTTP_TIMEOUT = 60
|
||||||
|
|
33
anki/sync.py
33
anki/sync.py
|
@ -382,11 +382,20 @@ class LocalServer(Syncer):
|
||||||
# HTTP syncing tools
|
# 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):
|
class HttpSyncer(object):
|
||||||
|
|
||||||
def _vars(self):
|
def _vars(self):
|
||||||
return dict(k=self.hkey)
|
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
|
# Posting data as a file
|
||||||
######################################################################
|
######################################################################
|
||||||
# We don't want to post the payload as a form var, as the percent-encoding is
|
# 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()
|
buf.close()
|
||||||
resp, cont = http.request(
|
resp, cont = http.request(
|
||||||
SYNC_URL+method, "POST", headers=headers, body=body)
|
SYNC_URL+method, "POST", headers=headers, body=body)
|
||||||
if resp['status'] != '200':
|
self.assertOk(resp)
|
||||||
raise Exception("Invalid response code: %s" % resp['status'])
|
|
||||||
return cont
|
return cont
|
||||||
|
|
||||||
# Incremental sync over HTTP
|
# Incremental sync over HTTP
|
||||||
|
@ -452,15 +460,12 @@ class RemoteServer(Syncer, HttpSyncer):
|
||||||
pw = pw.encode("utf-8")
|
pw = pw.encode("utf-8")
|
||||||
resp, cont = self.con.request(
|
resp, cont = self.con.request(
|
||||||
SYNC_URL+"hostKey?" + urllib.urlencode(dict(u=user,p=pw)))
|
SYNC_URL+"hostKey?" + urllib.urlencode(dict(u=user,p=pw)))
|
||||||
if resp['status'] == '200':
|
if resp['status'] == '403':
|
||||||
self.hkey = simplejson.loads(cont)['key']
|
|
||||||
return self.hkey
|
|
||||||
elif resp['status'] == '403':
|
|
||||||
# invalid auth
|
# invalid auth
|
||||||
return
|
return
|
||||||
else:
|
self.assertOk(resp)
|
||||||
raise Exception("Unknown response code: %s" % resp['status'])
|
self.hkey = simplejson.loads(cont)['key']
|
||||||
return
|
return self.hkey
|
||||||
|
|
||||||
def meta(self):
|
def meta(self):
|
||||||
resp, cont = self.con.request(
|
resp, cont = self.con.request(
|
||||||
|
@ -468,12 +473,7 @@ class RemoteServer(Syncer, HttpSyncer):
|
||||||
if resp['status'] == '403':
|
if resp['status'] == '403':
|
||||||
# auth failure
|
# auth failure
|
||||||
return
|
return
|
||||||
elif resp['status'] in ('503', '504'):
|
self.assertOk(resp)
|
||||||
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'])
|
|
||||||
return simplejson.loads(cont)
|
return simplejson.loads(cont)
|
||||||
|
|
||||||
def applyChanges(self, **kw):
|
def applyChanges(self, **kw):
|
||||||
|
@ -511,8 +511,7 @@ class FullSyncer(HttpSyncer):
|
||||||
self.col.close()
|
self.col.close()
|
||||||
resp, cont = self.con.request(
|
resp, cont = self.con.request(
|
||||||
SYNC_URL+"download?" + urllib.urlencode(self._vars()))
|
SYNC_URL+"download?" + urllib.urlencode(self._vars()))
|
||||||
if resp['status'] != '200':
|
self.assertOk(resp)
|
||||||
raise Exception("Invalid response code: %s" % resp['status'])
|
|
||||||
tpath = self.col.path + ".tmp"
|
tpath = self.col.path + ".tmp"
|
||||||
open(tpath, "wb").write(cont)
|
open(tpath, "wb").write(cont)
|
||||||
# check the received file is ok
|
# check the received file is ok
|
||||||
|
|
Loading…
Reference in a new issue