From ac89f2fa72732e9985ec1c3032afc2d63de1aaa7 Mon Sep 17 00:00:00 2001 From: Antoine Millet <antoine.millet@smartjog.com> Date: Fri, 7 Oct 2011 16:59:14 +0200 Subject: [PATCH] Reworked exception tree. RpcError are now reserved to rpc protocol, the RpcConnection uses the new RpcConnectionError class. --- sjrpc/core/exceptions.py | 8 ++++---- sjrpc/core/protocols/rpc.py | 10 +++++----- sjrpc/core/rpcconnection.py | 8 ++++---- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/sjrpc/core/exceptions.py b/sjrpc/core/exceptions.py index d7def24..67b7183 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 84b2104..5cac8ef 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 254d5a2..5598aa6 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) -- GitLab