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"
__author__ = "Jack Robison"
__maintainer__ = "Jack Robison"

View file

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

View file

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