1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-06-07 19:03:23 +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.session.login(config.get('username'), config.get('password'))
self.status['text'] = '' self.status['text'] = ''
except companion.VerificationRequired: except companion.VerificationRequired:
# don't worry about authentication now # don't worry about authentication now - prompt on query
self.status['text'] = '' self.status['text'] = ''
except companion.ServerError as e:
self.status['text'] = str(e)
except Exception as e: except Exception as e:
if __debug__: print_exc()
self.status['text'] = str(e) self.status['text'] = str(e)
self.cooldown() self.cooldown()
@ -144,36 +147,34 @@ class AppWindow:
# Validation # Validation
if not data.get('commander') or not data['commander'].get('name','').strip(): 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'): 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(): 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'): elif not data.get('lastStarport') or not data['lastStarport'].get('commodities'):
raise Exception("Station doesn't have a market!") self.status['text'] = "Station doesn't have a market!"
else:
if config.getint('output') & config.OUT_CSV: if config.getint('output') & config.OUT_CSV:
bpc.export(data, True) bpc.export(data, True)
if config.getint('output') & config.OUT_TD:
if config.getint('output') & config.OUT_TD: td.export(data)
td.export(data) if config.getint('output') & config.OUT_BPC:
bpc.export(data, False)
if config.getint('output') & config.OUT_BPC: if config.getint('output') & config.OUT_EDDN:
bpc.export(data, False) eddn.export(data, self.setstatus)
self.status['text'] = strftime('Last updated at %H:%M:%S', localtime(querytime))
if config.getint('output') & config.OUT_EDDN:
eddn.export(data, self.setstatus)
except companion.VerificationRequired: except companion.VerificationRequired:
return prefs.AuthenticationDialog(self.w, self.verify) return prefs.AuthenticationDialog(self.w, self.verify)
except companion.ServerError as e:
self.status['text'] = str(e)
except Exception as e: except Exception as e:
if __debug__: print_exc() if __debug__: print_exc()
self.status['text'] = str(e) self.status['text'] = str(e)
else:
self.status['text'] = strftime('Last updated at %H:%M:%S', localtime(querytime))
self.cooldown() self.cooldown()
def cooldown(self): def cooldown(self):

View File

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