diff --git a/sjrpc/core/exceptions.py b/sjrpc/core/exceptions.py index d7def2405885bdb6f886fbc543770c69e53bedff..67b7183bc04e6a0cf367db65dc2fa0c44f7c843c 100644 --- a/sjrpc/core/exceptions.py +++ b/sjrpc/core/exceptions.py @@ -18,17 +18,17 @@ class RpcError(Exception): return '%s' % self.message -class SocketRpcError(Exception): +class RpcConnectionError(Exception): ''' - Exception used internally to raise a socket fault. + Base class for RpcConnection errors. ''' -class RpcConnectionError(Exception): +class SocketError(RpcConnectionError): ''' - Base class for RpcConnection errors. + Exception used internally to raise a socket fault. ''' diff --git a/sjrpc/core/protocols/rpc.py b/sjrpc/core/protocols/rpc.py index 84b2104206a93920519b4b8ea55eb5c9459a1c3a..5cac8ef77e7d047936414090bb82a3e0b67e6746 100644 --- a/sjrpc/core/protocols/rpc.py +++ b/sjrpc/core/protocols/rpc.py @@ -5,7 +5,7 @@ from uuid import uuid4 from threading import Event, Thread -from sjrpc.core.exceptions import RpcError +from sjrpc.core.exceptions import RpcError, RpcConnectionError from sjrpc.core.protocols import Protocol __all__ = ['RpcProtocol'] @@ -208,12 +208,12 @@ class RpcProtocol(Protocol): Message must be a jsonisable structure. ''' - #if not self._connected: #FIXME - # raise RpcError('SendError', 'disconnected from the peer') - self.logger.debug('Sending: %s', message) json_msg = json.dumps(message) - self._connection.send(self._label, payload=json_msg) + try: + self._connection.send(self._label, payload=json_msg) + except RpcConnectionError as err: + raise RpcError('RpcConnectionError', str(err)) def _send_call(self, method_name, *args, **kwargs): ''' diff --git a/sjrpc/core/rpcconnection.py b/sjrpc/core/rpcconnection.py index 254d5a2de3346996f79262f059a790bbf9dce3b5..5598aa6089a48b6ed03013f454309dccb18447eb 100644 --- a/sjrpc/core/rpcconnection.py +++ b/sjrpc/core/rpcconnection.py @@ -14,8 +14,8 @@ import logging import threading from sjrpc.core.protocols import Protocol, RpcProtocol, TunnelProtocol -from sjrpc.core.exceptions import (RpcError, NoFreeLabelError, - FallbackModeEnabledError) +from sjrpc.core.exceptions import (RpcConnectionError, NoFreeLabelError, + FallbackModeEnabledError, SocketError) import pyev @@ -293,7 +293,7 @@ class RpcConnection(object): except socket.error as err: errmsg = 'Fatal error while sending through socket: %s' % err self.logger.error(errmsg) - raise RpcError('SocketError', errmsg) + raise SocketError(errmsg) self._outbound_buffer = self._outbound_buffer[sent:] if not self._outbound_buffer: @@ -334,7 +334,7 @@ class RpcConnection(object): ''' self._event_fallback.wait() if not self._connected: - raise RpcError('RpcError', 'Not connected to the peer') + raise RpcConnectionError('Not connected to the peer') size = len(payload) if self.fallback: header = struct.pack(RpcConnection.MESSAGE_HEADER_FALLBACK, size)