1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-04-14 00:07:14 +03:00

Handle server error more gracefully.

This commit is contained in:
Jonathan Harris 2015-06-05 18:05:24 +01:00
parent ad04a2c416
commit bbdacf3a31
2 changed files with 48 additions and 25 deletions

View File

@ -106,9 +106,12 @@ class AppWindow:
self.session.login(config.get('username'), config.get('password'))
self.status['text'] = ''
except companion.VerificationRequired:
# don't worry about authentication now
# don't worry about authentication now - prompt on query
self.status['text'] = ''
except companion.ServerError as e:
self.status['text'] = str(e)
except Exception as e:
if __debug__: print_exc()
self.status['text'] = str(e)
self.cooldown()
@ -144,36 +147,34 @@ class AppWindow:
# Validation
if not data.get('commander') or not data['commander'].get('name','').strip():
raise Exception("Who are you?!") # Shouldn't happen
self.status['text'] = "Who are you?!" # Shouldn't happen
elif not data['commander'].get('docked'):
raise Exception("You're not docked at a station!")
self.status['text'] = "You're not docked at a station!"
elif not data.get('lastSystem') or not data['lastSystem'].get('name','').strip():
raise Exception("Where are you?!") # Shouldn't happen
self.status['text'] = "Where are you?!" # Shouldn't happen
elif not data.get('lastStarport') or not data['lastStarport'].get('commodities'):
raise Exception("Station doesn't have a market!")
if config.getint('output') & config.OUT_CSV:
bpc.export(data, True)
if config.getint('output') & config.OUT_TD:
td.export(data)
if config.getint('output') & config.OUT_BPC:
bpc.export(data, False)
if config.getint('output') & config.OUT_EDDN:
eddn.export(data, self.setstatus)
self.status['text'] = "Station doesn't have a market!"
else:
if config.getint('output') & config.OUT_CSV:
bpc.export(data, True)
if config.getint('output') & config.OUT_TD:
td.export(data)
if config.getint('output') & config.OUT_BPC:
bpc.export(data, False)
if config.getint('output') & config.OUT_EDDN:
eddn.export(data, self.setstatus)
self.status['text'] = strftime('Last updated at %H:%M:%S', localtime(querytime))
except companion.VerificationRequired:
return prefs.AuthenticationDialog(self.w, self.verify)
except companion.ServerError as e:
self.status['text'] = str(e)
except Exception as e:
if __debug__: print_exc()
self.status['text'] = str(e)
else:
self.status['text'] = strftime('Last updated at %H:%M:%S', localtime(querytime))
self.cooldown()
def cooldown(self):

View File

@ -40,6 +40,10 @@ bracketmap = { 1: 'Low',
3: 'High', }
class ServerError(Exception):
def __str__(self):
return 'Error: Server is down'
class CredentialsError(Exception):
def __str__(self):
return 'Error: Invalid Credentials'
@ -79,9 +83,15 @@ class Session:
else:
self.credentials = { 'email' : username, 'password' : password }
r = self.session.post('https://companion.orerve.net/user/login', data = self.credentials)
if r.status_code != requests.codes.ok:
self.dump(r)
r.raise_for_status()
if 'Password' in r.text:
if 'server error' in r.text:
self.dump(r)
raise ServerError()
elif 'Password' in r.text:
self.dump(r)
raise CredentialsError()
elif 'Verification Code' in r.text:
self.state = Session.STATE_AUTH
@ -104,18 +114,24 @@ class Session:
if self.state == Session.STATE_NONE:
raise Exception('General error') # Shouldn't happen
elif self.state == Session.STATE_INIT:
raise CredentialsError()
self.login()
elif self.state == Session.STATE_AUTH:
raise VerificationRequired()
r = self.session.get('https://companion.orerve.net/profile')
if r.status_code != requests.codes.ok:
self.dump(r)
if r.status_code == requests.codes.forbidden:
# Maybe our session cookie expired?
# Start again - maybe our session cookie expired?
self.login()
r = self.session.get('https://companion.orerve.net/profile')
self.query()
r.raise_for_status()
return json.loads(r.text)
try:
return json.loads(r.text)
except:
self.dump(r)
raise ServerError()
def close(self):
self.state = Session.STATE_NONE
@ -125,3 +141,9 @@ class Session:
except:
pass
self.session = None
def dump(self, r):
if __debug__:
print 'Status\t%s' % r.status_code
print 'Headers\t%s' % r.headers
print ('Content:\n%s' % r.text).encode('utf-8')