also adds MultiError to libp2p/exceptions.py

and an additional fixme I have noticed
This commit is contained in:
Jonathan de Jong 2019-12-17 12:00:11 +01:00
parent ef31f7f6d6
commit 3cbe24caab
2 changed files with 46 additions and 4 deletions

View File

@ -8,3 +8,9 @@ class ValidationError(BaseLibp2pError):
class ParseError(BaseLibp2pError): class ParseError(BaseLibp2pError):
pass pass
class MultiError(BaseLibp2pError):
"""Raised with multiple exceptions."""
# todo: find some way for this to fancy-print all encapsulated errors

View File

@ -19,6 +19,7 @@ from libp2p.transport.transport_interface import ITransport
from libp2p.transport.upgrader import TransportUpgrader from libp2p.transport.upgrader import TransportUpgrader
from libp2p.typing import StreamHandlerFn from libp2p.typing import StreamHandlerFn
from ..exceptions import MultiError
from .connection.raw_connection import RawConnection from .connection.raw_connection import RawConnection
from .connection.swarm_connection import SwarmConn from .connection.swarm_connection import SwarmConn
from .exceptions import SwarmException from .exceptions import SwarmException
@ -90,14 +91,49 @@ class Swarm(INetwork):
except PeerStoreError: except PeerStoreError:
raise SwarmException(f"No known addresses to peer {peer_id}") raise SwarmException(f"No known addresses to peer {peer_id}")
if not addrs: if len(addrs) == 0:
raise SwarmException(f"No known addresses to peer {peer_id}") raise SwarmException(f"No known addresses to peer {peer_id}")
multiaddr = addrs[0] exceptions: List[SwarmException] = []
# Try all known addresses
for multiaddr in addrs:
try:
return await self.dial_addr(multiaddr, peer_id)
except SwarmException as e:
exceptions.append(e)
logger.debug(
"encountered swarm exception when trying to connect to %s, "
"trying next address...",
multiaddr,
exc_info=e,
)
# Tried all addresses, raising exception.
if len(exceptions) > 0:
raise SwarmException(
"unable to connect to %s, all addresses failed to dial (with exceptions)",
peer_id,
) from MultiError(exceptions)
else:
raise SwarmException(
"unable to connect to %s, all addresses failed to dial", peer_id
)
async def dial_addr(self, addr: Multiaddr, peer_id: ID) -> INetConn:
"""
dial_addr try to create a connection to peer_id with addr.
:param addr: the address we want to connect with
:param peer_id: the peer we want to connect to
:raises SwarmException: raised when an error occurs
:return: muxed connection
"""
# Dial peer (connection to peer does not yet exist) # Dial peer (connection to peer does not yet exist)
# Transport dials peer (gets back a raw conn) # Transport dials peer (gets back a raw conn)
try: try:
raw_conn = await self.transport.dial(multiaddr) raw_conn = await self.transport.dial(addr)
except OpenConnectionError as error: except OpenConnectionError as error:
logger.debug("fail to dial peer %s over base transport", peer_id) logger.debug("fail to dial peer %s over base transport", peer_id)
raise SwarmException( raise SwarmException(
@ -137,7 +173,7 @@ class Swarm(INetwork):
async def new_stream(self, peer_id: ID) -> INetStream: async def new_stream(self, peer_id: ID) -> INetStream:
""" """
:param peer_id: peer_id of destination :param peer_id: peer_id of destination
:param protocol_id: protocol id :param protocol_id: protocol id fixme: protocol_id not in parameters
:raises SwarmException: raised when an error occurs :raises SwarmException: raised when an error occurs
:return: net stream instance :return: net stream instance
""" """