Changes try-except-blocks around maybe_nody_by_*() calls to only catch KeyError exceptions.

Semantically, all the implemented error handling for the try-except-
blocks around calls to "maybe_node_by_mac()" or "maybe_node_by_id()" in
nodedb.py only handle the case that a particular MAC address cannot be
found in the list of known nodes. If such a MAC address cannot be found
in this list, the methods properly indicate this by raising a KeyError.
However, all the try-except-block generically catch all exceptions and
thus may cover other problems. But not only that problems might be
covered by this, generic try-except-blocks make finding errors and de-
bugging quite painful.
Hence, these try-except-blocks should only catch KeyErrors or at least
have an error handling that differs from other exceptions.
This commit is contained in:
Stefan Laudemann 2015-02-01 13:50:01 +01:00
parent 94f7256564
commit b3c629264a

View file

@ -46,7 +46,7 @@ class NodeDB:
for n in obj: for n in obj:
try: try:
node = self.maybe_node_by_id(n['id']) node = self.maybe_node_by_id(n['id'])
except: except KeyError:
node = Node() node = Node()
node.id = n['id'] node.id = n['id']
node.name = n['name'] node.name = n['name']
@ -81,7 +81,7 @@ class NodeDB:
if 'of' in x: if 'of' in x:
try: try:
node = self.maybe_node_by_mac((x['of'], x['secondary'])) node = self.maybe_node_by_mac((x['of'], x['secondary']))
except: except KeyError:
node = Node() node = Node()
node.lastseen = self.time node.lastseen = self.time
node.firstseen = self.time node.firstseen = self.time
@ -99,7 +99,7 @@ class NodeDB:
try: try:
node = self.maybe_node_by_mac((x['router'], )) node = self.maybe_node_by_mac((x['router'], ))
except: except KeyError:
node = Node() node = Node()
node.lastseen = self.time node.lastseen = self.time
node.firstseen = self.time node.firstseen = self.time
@ -111,14 +111,14 @@ class NodeDB:
if 'neighbor' in x: if 'neighbor' in x:
try: try:
node = self.maybe_node_by_mac((x['neighbor'], )) node = self.maybe_node_by_mac((x['neighbor'], ))
except: except KeyError:
continue continue
if 'gateway' in x: if 'gateway' in x:
x['neighbor'] = x['gateway'] x['neighbor'] = x['gateway']
node = self.maybe_node_by_mac((x['neighbor'], )) node = self.maybe_node_by_mac((x['neighbor'], ))
except: except KeyError:
node = Node() node = Node()
node.lastseen = self.time node.lastseen = self.time
node.firstseen = self.time node.firstseen = self.time
@ -138,7 +138,7 @@ class NodeDB:
router = self.maybe_node_by_mac((x['router'], )) router = self.maybe_node_by_mac((x['router'], ))
neighbor = self.maybe_node_by_mac((x['neighbor'], )) neighbor = self.maybe_node_by_mac((x['neighbor'], ))
except: except KeyError:
continue continue
# filter TT links merged in previous step # filter TT links merged in previous step
@ -161,7 +161,7 @@ class NodeDB:
if 'primary' in x: if 'primary' in x:
try: try:
node = self.maybe_node_by_mac((x['primary'], )) node = self.maybe_node_by_mac((x['primary'], ))
except: except KeyError:
continue continue
node.id = x['primary'] node.id = x['primary']
@ -172,9 +172,9 @@ class NodeDB:
node = self.maybe_node_by_mac((x['router'], )) node = self.maybe_node_by_mac((x['router'], ))
node.add_mac(x['gateway']) node.add_mac(x['gateway'])
node.clientcount += 1 node.clientcount += 1
except: except KeyError:
pass pass
# don't count node as its own client # don't count node as its own client
for node in self._nodes: for node in self._nodes:
if node.clientcount > 0: if node.clientcount > 0:
@ -207,7 +207,7 @@ class NodeDB:
for mac, alias in aliases.items(): for mac, alias in aliases.items():
try: try:
node = self.maybe_node_by_mac([mac]) node = self.maybe_node_by_mac([mac])
except: except KeyError:
# create an offline node # create an offline node
node = Node() node = Node()
node.add_mac(mac) node.add_mac(mac)