Replace errors with exceptions

This commit is contained in:
Stuckinaboot 2018-10-29 13:13:39 +01:00
parent 2544f8dff5
commit e93ced5f6a
4 changed files with 18 additions and 12 deletions

View File

@ -28,4 +28,5 @@ class PeerData(IPeerData):
def get_metadata(self, key): def get_metadata(self, key):
if key in self.metadata: if key in self.metadata:
return self.metadata[key], None return self.metadata[key], None
return None, "key not found" else:
raise Exception("key not found")

View File

@ -41,7 +41,8 @@ class IPeerData(ABC):
""" """
:param key: key in KV pair :param key: key in KV pair
:return: val for key, error (only defined if key not found) :return: val for key
:raise Exception: key not found exception
""" """
def get_metadata(self, key): def get_metadata(self, key):
pass pass

View File

@ -32,8 +32,9 @@ class PeerStore(IPeerStore):
def get_protocols(self, peer_id): def get_protocols(self, peer_id):
if peer_id in self.peer_map: if peer_id in self.peer_map:
return self.peer_map[peer_id].get_protocols(), None return self.peer_map[peer_id].get_protocols()
return None, peer_id + " not found" else:
raise Exception("peer ID not found")
def add_protocols(self, peer_id, protocols): def add_protocols(self, peer_id, protocols):
peer = self.__create_or_get_peer(peer_id) peer = self.__create_or_get_peer(peer_id)
@ -44,9 +45,10 @@ class PeerStore(IPeerStore):
def get(self, peer_id, key): def get(self, peer_id, key):
if peer_id in self.peer_map: if peer_id in self.peer_map:
val, error = self.peer_map[peer_id].get_metadata(key) val = self.peer_map[peer_id].get_metadata(key)
return val, error return val
return None, peer_id + " not found" else:
raise Exception("peer ID not found")
def put(self, peer_id, key, val): def put(self, peer_id, key, val):
# <<?>> # <<?>>
@ -64,8 +66,9 @@ class PeerStore(IPeerStore):
def addrs(self, peer_id): def addrs(self, peer_id):
if peer_id in self.peer_map: if peer_id in self.peer_map:
return self.peer_map[peer_id].get_addrs(), None return self.peer_map[peer_id].get_addrs()
return None, peer_id + " not found" else:
raise Exception("peer ID not found")
def clear_addrs(self, peer_id): def clear_addrs(self, peer_id):
# Only clear addresses if the peer is in peer map # Only clear addresses if the peer is in peer map

View File

@ -20,7 +20,8 @@ class IPeerStore(ABC, IAddrBook, IPeerMetadata):
def get_protocols(self, peer_id): def get_protocols(self, peer_id):
""" """
:param peer_id: peer ID to get protocols for :param peer_id: peer ID to get protocols for
:return: protocols (as strings), error :return: protocols (as strings)
:raise Exception: peer ID not found exception
""" """
pass pass
@ -29,7 +30,7 @@ class IPeerStore(ABC, IAddrBook, IPeerMetadata):
""" """
:param peer_id: peer ID to add protocols for :param peer_id: peer ID to add protocols for
:param protocols: protocols to add :param protocols: protocols to add
:return: error :raise Exception: peer ID not found exception
""" """
pass pass
@ -38,7 +39,7 @@ class IPeerStore(ABC, IAddrBook, IPeerMetadata):
""" """
:param peer_id: peer ID to set protocols for :param peer_id: peer ID to set protocols for
:param protocols: protocols to set :param protocols: protocols to set
:return: error :raise Exception: peer ID not found exception
""" """
pass pass