fix raising remote exceptions

This commit is contained in:
Jack Robison 2017-10-10 13:09:25 -04:00
parent 4a567f7ab1
commit f1980f524e
No known key found for this signature in database
GPG key ID: 284699E7404E3CFF
2 changed files with 14 additions and 2 deletions

View file

@ -1,4 +1,11 @@
import binascii
import exceptions
# this is a dict of {"exceptions.<exception class name>": 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):

View file

@ -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