Improve networking exceptions

This commit is contained in:
tomkat83 2017-07-27 19:16:14 +02:00
parent b698d2e0e1
commit 0f84836533

View file

@ -196,7 +196,7 @@ class ConnectionManager(object):
r = self._requests(action, **request) r = self._requests(action, **request)
log.info("ConnectionManager response status: %s" % r.status_code) log.info("ConnectionManager response status: %s" % r.status_code)
r.raise_for_status() r.raise_for_status()
except Exception as e: except requests.RequestException as e:
# Elaborate on exceptions? # Elaborate on exceptions?
log.error(e) log.error(e)
raise raise
@ -205,8 +205,8 @@ class ConnectionManager(object):
return etree.fromstring(r.content) return etree.fromstring(r.content)
except etree.ParseError: except etree.ParseError:
# Read response to release connection # Read response to release connection
r.content log.error('Could not parse PMS response: %s' % r.content)
raise raise requests.RequestException
def _requests(self, action, **kwargs): def _requests(self, action, **kwargs):
@ -258,15 +258,18 @@ class ConnectionManager(object):
""" """
PlexGDM PlexGDM
""" """
# setup socket for discovery -> multicast message
GDM = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
GDM.settimeout(2.0)
# Set the time-to-live for messages to 2 for local network
ttl = pack('b', 2)
GDM.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl)
servers = [] servers = []
# setup socket for discovery -> multicast message
try:
GDM = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
GDM.settimeout(2.0)
# Set the time-to-live for messages to 2 for local network
ttl = pack('b', 2)
GDM.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl)
except (socket.error, socket.herror, socket.gaierror):
log.error('Socket error, abort PlexGDM')
return servers
try: try:
# Send data to the multicast group # Send data to the multicast group
GDM.sendto(MSG_PLEXGDM, (IP_PLEXGDM, PORT_PLEXGDM)) GDM.sendto(MSG_PLEXGDM, (IP_PLEXGDM, PORT_PLEXGDM))
@ -279,7 +282,7 @@ class ConnectionManager(object):
break break
except: except:
# Probably error: (101, 'Network is unreachable') # Probably error: (101, 'Network is unreachable')
log.error('Could not find Plex servers using GDM') log.error('Could not find Plex servers using PlexGDM')
import traceback import traceback
log.error("Traceback:\n%s" % traceback.format_exc()) log.error("Traceback:\n%s" % traceback.format_exc())
finally: finally:
@ -349,15 +352,14 @@ class ConnectionManager(object):
Retrieves Plex Media Servers from plex.tv/pms/resources Retrieves Plex Media Servers from plex.tv/pms/resources
""" """
servers = [] servers = []
xml = self.requestUrl({
'url': 'https://plex.tv/api/resources?includeHttps=1',
'type': 'GET',
'headers': {'X-Plex-Token': self.plexToken},
'timeout': 5.0,
'verify': True})
try: try:
xml.attrib xml = self.requestUrl({
except AttributeError: 'url': 'https://plex.tv/api/resources?includeHttps=1',
'type': 'GET',
'headers': {'X-Plex-Token': self.plexToken},
'timeout': 5.0,
'verify': True})
except requests.RequestException:
log.error('Could not get list of PMS from plex.tv') log.error('Could not get list of PMS from plex.tv')
return servers return servers