Some (bad) network error handling logic, borrowed from eveapi

This commit is contained in:
blitzmann
2014-08-14 22:52:27 -04:00
parent ed1b9854a0
commit 1279b20370
2 changed files with 56 additions and 20 deletions

View File

@@ -18,7 +18,7 @@
#===============================================================================
import wx
import bitmapLoader
import gui.mainFrame
import wx.lib.newevent
import wx.gizmos
@@ -27,7 +27,6 @@ import service
import gui.display as d
from gui.contextMenu import ContextMenu
from wx.lib.buttons import GenBitmapButton
import sys
import gui.globalEvents as GE
class CharacterEditor(wx.Frame):
@@ -638,22 +637,25 @@ class APIView (wx.Panel):
return
sChar = service.Character.getInstance()
list = sChar.charList(self.Parent.Parent.getActiveCharacter(), self.inputID.GetLineText(0), self.inputKey.GetLineText(0))
try:
list = sChar.charList(self.Parent.Parent.getActiveCharacter(), self.inputID.GetLineText(0), self.inputKey.GetLineText(0))
except service.network.AuthenticationError, e:
self.stStatus.SetLabel("%s\nAuthentication failure. Please check keyID and vCode combination."%e)
except service.network.TimeoutError, e:
self.stStatus.SetLabel("Request timed out. Please check network connectivity and/or proxy settings.")
except Exception, e:
self.stStatus.SetLabel("Error:\n%s"%e)
else:
self.charChoice.Clear()
for charName in list:
i = self.charChoice.Append(charName)
if not list:
self.stStatus.SetLabel("Unable to fetch characters list from EVE API!")
return
self.btnFetchSkills.Enable(True)
self.charChoice.Enable(True)
self.charChoice.Clear()
for charName in list:
i = self.charChoice.Append(charName)
self.Layout()
self.btnFetchSkills.Enable(True)
self.charChoice.Enable(True)
self.Layout()
self.charChoice.SetSelection(0)
self.charChoice.SetSelection(0)
def fetchSkills(self, event):
charName = self.charChoice.GetString(self.charChoice.GetSelection())
@@ -662,5 +664,5 @@ class APIView (wx.Panel):
sChar = service.Character.getInstance()
sChar.apiFetch(self.Parent.Parent.getActiveCharacter(), charName)
self.stStatus.SetLabel("Successfully fetched %s\'s skills from EVE API." % charName)
except:
self.stStatus.SetLabel("Unable to retrieve %s\'s skills!" % charName)
except Exception, e:
self.stStatus.SetLabel("Unable to retrieve %s\'s skills. Error message:\n%s" % (charName, e))

View File

@@ -21,6 +21,27 @@ from service.settings import NetworkSettings
import urllib2
import urllib
import config
import socket
# network timeout, otherwise pyfa hangs for a long while if no internet connection
timeout = 3
socket.setdefaulttimeout(timeout)
class Error(StandardError):
def __init__(self, error):
self.error = error
class RequestError(StandardError):
pass
class AuthenticationError(StandardError):
pass
class ServerError(StandardError):
pass
class TimeoutError(StandardError):
pass
class Network():
# Request constants - every request must supply this, as it is checked if
@@ -62,6 +83,19 @@ class Network():
urllib2.install_opener(opener)
request = urllib2.Request(url, headers=headers, data=urllib.urlencode(postData) if postData else None)
data = urllib2.urlopen(request)
print "\tReturning data"
return data
try:
data = urllib2.urlopen(request)
print "\tReturning data"
return data
except urllib2.HTTPError, error:
if error.code == 404:
raise RequestError(error)
elif error.code == 403:
raise AuthenticationError(error)
elif error.code >= 500:
raise ServerError(error)
except urllib2.URLError, error:
if "timed out" in error.reason:
raise TimeoutError(error)
else:
raise Error(error)