From 2a83b1a61b15372d07851de282d4059d7ed37386 Mon Sep 17 00:00:00 2001 From: "Srivats P." Date: Thu, 12 Jun 2014 07:11:32 +0530 Subject: [PATCH] CLI: cleaned up exceptions to make code more readable --- binding/rpc.py | 130 +++++++++++++++++++++++++++---------------------- 1 file changed, 72 insertions(+), 58 deletions(-) diff --git a/binding/rpc.py b/binding/rpc.py index 9e3be2d..791ef1d 100644 --- a/binding/rpc.py +++ b/binding/rpc.py @@ -22,6 +22,27 @@ import socket import struct import sys +class PeerClosedConnError(Exception): + def __init__(self, msg): + self.msg = msg + def __str__(self): + return self.msg + +class RpcError(Exception): + def __init__(self, msg): + self.msg = msg + def __str__(self): + return self.msg + +class RpcMismatchError(Exception): + def __init__(self, msg, received, expected): + self.msg = msg + self.received = received + self.expected = expected + def __str__(self): + return '%s - Expected method %d, Received method %d' % ( + self.msg, self.expected, self.received) + class OstinatoRpcController(RpcController): def __init__(self): super(OstinatoRpcController, self).__init__() @@ -35,9 +56,10 @@ class OstinatoRpcChannel(RpcChannel): try: self.sock.connect((host, port)) except socket.error, e: - print 'ERROR: Unable to connect to Drone %s (%s)' % ( + error = 'ERROR: Unable to connect to Drone %s (%s)' % ( self.peer, str(e)) - sys.exit(1) + print error + raise def disconnect(self): self.sock.close() @@ -48,83 +70,75 @@ class OstinatoRpcChannel(RpcChannel): OST_PB_MSG_TYPE_RESPONSE = 2 OST_PB_MSG_TYPE_BLOB = 3 + error = '' try: req = request.SerializeToString() self.sock.sendall(struct.pack('>HHI', OST_PB_MSG_TYPE_REQUEST, method.index, len(req)) + req) - except EncodeError, e: - print 'ERROR: Failed to serialize %s arg for RPC %s() ' \ - 'to Drone %s (%s)' % ( - type(request).__name__, method.name, self.peer, e) - sys.exit(1) - except socket.error, e: - print 'ERROR: Failed to invoke RPC %s() to Drone %s (%s)' % ( - method.name, self.peer, e) - sys.exit(1) - # receive and parse header - try: + # receive and parse header hdr = '' while len(hdr) < OST_PB_MSG_HDR_SIZE: chunk = self.sock.recv(OST_PB_MSG_HDR_SIZE - len(hdr)) if chunk == '': - raise RuntimeError("socket connection closed by peer") + raise PeerClosedConnError('connection closed by peer') hdr = hdr + chunk - except socket.error, e: - print 'ERROR: Failed to receive msg reply for RPC %s() ' \ - 'from Drone %s (%s)' % ( - method.name, self.peer, e) - sys.exit(1) - except RuntimeError, e: - print 'ERROR: Drone %s closed connection receiving msg reply ' \ - 'for RPC %s() (%s)' % ( - self.peer, method.name, e) - sys.exit(1) - (msg_type, method_index, resp_len) = struct.unpack('>HHI', hdr) + (msg_type, method_index, resp_len) = struct.unpack('>HHI', hdr) - # verify response method is same as the one requested - if method_index != method.index: - print 'ERROR: Received Reply for Method %d; expecting reply for ' \ - 'method %d (%s)' % ( - method_index, method.index, method.name) - sys.exit(1) - - # receive and parse the actual response message - try: + # receive and parse the actual response message resp = '' while len(resp) < resp_len: chunk = self.sock.recv(resp_len - len(resp)) if chunk == '': - raise RuntimeError("socket connection closed by peer") + raise PeerClosedConnError('connection closed by peer') resp = resp + chunk + + # verify response method is same as the one requested + if method_index != method.index: + raise RpcMismatchError('RPC mismatch', + expected = method.index, received = method_index) + + if msg_type == OST_PB_MSG_TYPE_RESPONSE: + response = response_class() + response.ParseFromString(resp) + elif msg_type == OST_PB_MSG_TYPE_BLOB: + response = resp + else: + raise RpcError('unknown RPC msg type %d' % msg_type) + + controller.response = response + except socket.error, e: - print 'ERROR: Failed to receive reply for RPC %s() ' \ - 'from Drone %s (%s)' % ( + error = 'ERROR: RPC %s() to Drone %s failed (%s)' % ( method.name, self.peer, e) - sys.exit(1) - except RuntimeError, e: - print 'ERROR: Drone %s closed connection receiving reply ' \ + raise + except PeerClosedConnError, e: + error = 'ERROR: Drone %s closed connection receiving reply ' \ 'for RPC %s() (%s)' % ( self.peer, method.name, e) - sys.exit(1) - - if msg_type == OST_PB_MSG_TYPE_RESPONSE: - try: - response = response_class() - response.ParseFromString(resp) - except DecodeError, e: - print 'ERROR: Failed to parse %s response for RPC %s() ' \ - 'from Drone %s (%s)' % ( - type(response).__name__, method.name, self.peer, e) - sys.exit(1) - elif msg_type == OST_PB_MSG_TYPE_BLOB: - response = resp - else: - print 'ERROR: unsupported msg type %d received in respone to %s' % \ - msg_type, method.name - - controller.response = response + raise + except EncodeError, e: + error = 'ERROR: Failed to serialize %s arg for RPC %s() ' \ + 'to Drone %s (%s)' % ( + type(request).__name__, method.name, self.peer, e) + raise + except DecodeError, e: + error = 'ERROR: Failed to parse %s response for RPC %s() ' \ + 'from Drone %s (%s)' % ( + type(response).__name__, method.name, self.peer, e) + raise + except RpcMismatchError, e: + error = 'ERROR: Rpc Mismatch for RPC %s() (%s)' % ( + method.name, e) + raise + except RpcError, e: + error = 'ERROR: Unknown reply received for RPC %s() (%s) ' % ( + method.name, e) + raise + finally: + if error: + print error