1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-06-08 19:32:15 +03:00

More robust handling of cAPI server errors

This commit is contained in:
Jonathan Harris 2019-01-18 13:21:51 +00:00
parent b9bb293624
commit 8838dde116
2 changed files with 17 additions and 7 deletions

View File

@ -468,7 +468,7 @@ class AppWindow:
self.holdofftime = querytime + companion.holdoff self.holdofftime = querytime + companion.holdoff
# Companion API problem # Companion API problem
except (companion.ServerError, companion.ServerLagging) as e: except companion.ServerLagging as e:
if retrying: if retrying:
self.status['text'] = unicode(e) self.status['text'] = unicode(e)
play_bad = True play_bad = True
@ -483,7 +483,7 @@ class AppWindow:
companion.session.invalidate() companion.session.invalidate()
self.login() self.login()
except Exception as e: except Exception as e: # Including CredentialsError, ServerError
if __debug__: print_exc() if __debug__: print_exc()
self.status['text'] = unicode(e) self.status['text'] = unicode(e)
play_bad = True play_bad = True

View File

@ -271,6 +271,7 @@ class Session:
self.credentials = None self.credentials = None
self.session = None self.session = None
self.auth = None self.auth = None
self.retrying = False # Avoid infinite loop when successful auth / unsuccessful query
# yuck suppress InsecurePlatformWarning under Python < 2.7.9 which lacks SNI support # yuck suppress InsecurePlatformWarning under Python < 2.7.9 which lacks SNI support
if sys.version_info < (2,7,9): if sys.version_info < (2,7,9):
@ -343,10 +344,11 @@ class Session:
if __debug__: print_exc() if __debug__: print_exc()
raise ServerError() raise ServerError()
if 400 <= r.status_code < 500 or r.url.startswith(SERVER_AUTH): if r.url.startswith(SERVER_AUTH):
# Client error or redirected back to Auth server - force full re-authentication # Redirected back to Auth server - force full re-authentication
self.dump(r) self.dump(r)
self.invalidate() self.invalidate()
self.retrying = False
self.login() self.login()
raise CredentialsError() raise CredentialsError()
elif 500 <= r.status_code < 600: elif 500 <= r.status_code < 600:
@ -355,16 +357,24 @@ class Session:
raise ServerError() raise ServerError()
try: try:
data = r.json() # Will fail here if token expired since response is empty r.raise_for_status() # Typically 403 "Forbidden" on token expiry
data = r.json() # May also fail here if token expired since response is empty
except: except:
# Start again - maybe our token expired
self.dump(r) self.dump(r)
self.close() self.close()
if self.login(): if self.retrying: # Refresh just succeeded but this query failed! Force full re-authentication
self.invalidate()
self.retrying = False
self.login()
raise CredentialsError()
elif self.login(): # Maybe our token expired. Re-authorize in any case
self.retrying = True
return self.query(endpoint) return self.query(endpoint)
else: else:
self.retrying = False
raise CredentialsError() raise CredentialsError()
self.retrying = False
if 'timestamp' not in data: if 'timestamp' not in data:
data['timestamp'] = time.strftime('%Y-%m-%dT%H:%M:%SZ', parsedate(r.headers['Date'])) data['timestamp'] = time.strftime('%Y-%m-%dT%H:%M:%SZ', parsedate(r.headers['Date']))
return data return data