From b3c629264af75a2c0acebf9137497392ff1add03 Mon Sep 17 00:00:00 2001 From: Stefan Laudemann Date: Sun, 1 Feb 2015 13:50:01 +0100 Subject: [PATCH] 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. --- nodedb.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/nodedb.py b/nodedb.py index 63c461e..0bec73e 100644 --- a/nodedb.py +++ b/nodedb.py @@ -46,7 +46,7 @@ class NodeDB: for n in obj: try: node = self.maybe_node_by_id(n['id']) - except: + except KeyError: node = Node() node.id = n['id'] node.name = n['name'] @@ -81,7 +81,7 @@ class NodeDB: if 'of' in x: try: node = self.maybe_node_by_mac((x['of'], x['secondary'])) - except: + except KeyError: node = Node() node.lastseen = self.time node.firstseen = self.time @@ -99,7 +99,7 @@ class NodeDB: try: node = self.maybe_node_by_mac((x['router'], )) - except: + except KeyError: node = Node() node.lastseen = self.time node.firstseen = self.time @@ -111,14 +111,14 @@ class NodeDB: if 'neighbor' in x: try: node = self.maybe_node_by_mac((x['neighbor'], )) - except: + except KeyError: continue if 'gateway' in x: x['neighbor'] = x['gateway'] node = self.maybe_node_by_mac((x['neighbor'], )) - except: + except KeyError: node = Node() node.lastseen = self.time node.firstseen = self.time @@ -138,7 +138,7 @@ class NodeDB: router = self.maybe_node_by_mac((x['router'], )) neighbor = self.maybe_node_by_mac((x['neighbor'], )) - except: + except KeyError: continue # filter TT links merged in previous step @@ -161,7 +161,7 @@ class NodeDB: if 'primary' in x: try: node = self.maybe_node_by_mac((x['primary'], )) - except: + except KeyError: continue node.id = x['primary'] @@ -172,9 +172,9 @@ class NodeDB: node = self.maybe_node_by_mac((x['router'], )) node.add_mac(x['gateway']) node.clientcount += 1 - except: + except KeyError: pass - + # don't count node as its own client for node in self._nodes: if node.clientcount > 0: @@ -207,7 +207,7 @@ class NodeDB: for mac, alias in aliases.items(): try: node = self.maybe_node_by_mac([mac]) - except: + except KeyError: # create an offline node node = Node() node.add_mac(mac)