From f1980f524eac03eec6ca46c69f2b32101e48344e Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Tue, 10 Oct 2017 13:09:25 -0400 Subject: [PATCH] fix raising remote exceptions --- lbrynet/dht/error.py | 7 +++++++ lbrynet/dht/protocol.py | 9 +++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lbrynet/dht/error.py b/lbrynet/dht/error.py index 78daee46b..3111adf8f 100644 --- a/lbrynet/dht/error.py +++ b/lbrynet/dht/error.py @@ -1,4 +1,11 @@ import binascii +import exceptions + +# this is a dict of {"exceptions.": exception class} items used to raise +# remote built-in exceptions locally +BUILTIN_EXCEPTIONS = { + "exceptions.%s" % e: getattr(exceptions, e) for e in dir(exceptions) if not e.startswith("_") +} class DecodeError(Exception): diff --git a/lbrynet/dht/protocol.py b/lbrynet/dht/protocol.py index 17c1f2dd1..6ae7b98a3 100644 --- a/lbrynet/dht/protocol.py +++ b/lbrynet/dht/protocol.py @@ -11,7 +11,7 @@ import encoding import msgtypes import msgformat from contact import Contact -from error import UnknownRemoteException, TimeoutError +from error import BUILTIN_EXCEPTIONS, UnknownRemoteException, TimeoutError from delay import Delay log = logging.getLogger(__name__) @@ -246,7 +246,12 @@ class KademliaProtocol(protocol.DatagramProtocol): df.callback((message, address)) elif isinstance(message, msgtypes.ErrorMessage): # The RPC request raised a remote exception; raise it locally - remoteException = Exception(message.response) + if message.exceptionType in BUILTIN_EXCEPTIONS: + exception_type = BUILTIN_EXCEPTIONS[message.exceptionType] + else: + exception_type = UnknownRemoteException + remoteException = exception_type(message.response) + log.error("Remote exception (%s): %s", address, remoteException) df.errback(remoteException) else: # We got a result from the RPC