error handling

This commit is contained in:
Jack Robison 2018-08-21 12:10:11 -04:00
parent ba87ce9496
commit f8b1a1326a
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2
3 changed files with 22 additions and 18 deletions

View file

@ -1,4 +1,4 @@
__version__ = "0.0.1a10" __version__ = "0.0.1a11"
__name__ = "txupnp" __name__ = "txupnp"
__author__ = "Jack Robison" __author__ = "Jack Robison"
__maintainer__ = "Jack Robison" __maintainer__ = "Jack Robison"

View file

@ -58,10 +58,12 @@ class _SCPDCommand(object):
def extract_response(self, body): def extract_response(self, body):
body = handle_fault(body) # raises UPnPError if there is a fault body = handle_fault(body) # raises UPnPError if there is a fault
if '%sResponse' % self.method in body: response_key = None
response_key = '%sResponse' % self.method for key in body:
else: if self.method in key:
log.error(body.keys()) response_key = key
break
if not response_key:
raise UPnPError("unknown response fields") raise UPnPError("unknown response fields")
response = body[response_key] response = body[response_key]
extracted_response = tuple([response[n] for n in self.returns]) extracted_response = tuple([response[n] for n in self.returns])
@ -91,6 +93,8 @@ class _SCPDCommand(object):
xml_response = yield response.content() xml_response = yield response.content()
try: try:
response = self.extract_response(self.extract_body(xml_response)) response = self.extract_response(self.extract_body(xml_response))
except UPnPError:
raise
except Exception as err: except Exception as err:
log.debug("error extracting response (%s) to %s:\n%s", err, self.method, xml_response) log.debug("error extracting response (%s) to %s:\n%s", err, self.method, xml_response)
raise err raise err

View file

@ -109,20 +109,23 @@ class UPnP(object):
NewEnabled=1, NewPortMappingDescription=description, NewLeaseDuration="" NewEnabled=1, NewPortMappingDescription=description, NewLeaseDuration=""
) )
@defer.inlineCallbacks
def get_port_mapping_by_index(self, index): def get_port_mapping_by_index(self, index):
return self.commands.GetGenericPortMappingEntry(NewPortMappingIndex=index) try:
redirect = yield self.commands.GetGenericPortMappingEntry(NewPortMappingIndex=index)
defer.returnValue(redirect)
except UPnPError:
defer.returnValue(None)
@defer.inlineCallbacks @defer.inlineCallbacks
def get_redirects(self): def get_redirects(self):
redirects = [] redirects = []
cnt = 0 cnt = 0
while True: redirect = yield self.get_port_mapping_by_index(cnt)
try: while redirect:
redirect = yield self.get_port_mapping_by_index(cnt) redirects.append(redirect)
redirects.append(redirect) cnt += 1
cnt += 1 redirect = yield self.get_port_mapping_by_index(cnt)
except UPnPError:
break
defer.returnValue(redirects) defer.returnValue(redirects)
@defer.inlineCallbacks @defer.inlineCallbacks
@ -138,11 +141,8 @@ class UPnP(object):
NewRemoteHost=None, NewExternalPort=external_port, NewProtocol=protocol NewRemoteHost=None, NewExternalPort=external_port, NewProtocol=protocol
) )
defer.returnValue(result) defer.returnValue(result)
except UPnPError as err: except UPnPError:
if 'NoSuchEntryInArray' in str(err): defer.returnValue(None)
defer.returnValue(None)
else:
raise err
def delete_port_mapping(self, external_port, protocol, new_remote_host=""): def delete_port_mapping(self, external_port, protocol, new_remote_host=""):
""" """