From 92195ae21929b464a80b25906b80181384e3ae95 Mon Sep 17 00:00:00 2001 From: Mark Slee Date: Wed, 21 Feb 2007 05:16:30 +0000 Subject: [PATCH] Starting python exception handling cleanup Reviewed By: aditya git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665013 13f79535-47bb-0310-9956-ffa450edef68 --- lib/py/src/Thrift.py | 56 ++++++++++++++++++++++++++++++ lib/py/src/protocol/TProtocol.py | 15 ++++++++ lib/py/src/transport/TSocket.py | 9 +++-- lib/py/src/transport/TTransport.py | 13 +++++-- 4 files changed, 88 insertions(+), 5 deletions(-) diff --git a/lib/py/src/Thrift.py b/lib/py/src/Thrift.py index 1be84e29..81689b70 100644 --- a/lib/py/src/Thrift.py +++ b/lib/py/src/Thrift.py @@ -4,3 +4,59 @@ class TProcessor: def process(iprot, oprot): pass + +class TException(Exception): + + """Base class for all thrift exceptions.""" + + def __init__(self, message=None): + Exception.__init__(self, message) + +class TApplicationException(TException): + + """Application level thrift exceptions.""" + + UNKNOWN = 0 + UNKNOWN_METHOD = 1 + INVALID_MESSAGE_TYPE = 2 + WRONG_METHOD_NAME = 3 + BAD_SEQUENCE_ID = 4 + MISSING_RESULT = 5 + + def __init__(self, type=UNKNOWN, message=None): + TException.__init__(self, message) + self.type = type + + def read(self, iprot): + iprot.readStructBegin() + while True: + (fname, ftype, fid) = iprot.readFieldBegin() + if ftype == TType.STOP: + break + if fid == 1: + if ftype == TType.STRING: + self.message = iprot.readString(); + else: + iprot.skip(ftype) + elif fid == 2: + if ftype == TType.I32: + self.type = iprot.readI32(); + else: + iprot.skip(ftype) + else: + iprot.skip(ftype) + iprot.readFieldEnd() + iprot.readStructEnd() + + def write(self, oprot): + oprot.writeStructBegin('TApplicationException') + if self.message != None: + oprot.writeFieldBegin('message', TType.STRING, 1) + oprot.writeString(self.message) + oprot.writeFieldEnd() + if self.type != None: + oprot.writeFieldBegin('type', TType.I32, 2) + oprot.writeI32(self.type) + oprot.writeFieldEnd() + oprot.writeFieldStop() + oprot.writeStructEnd() diff --git a/lib/py/src/protocol/TProtocol.py b/lib/py/src/protocol/TProtocol.py index 15206b0d..e35688e1 100644 --- a/lib/py/src/protocol/TProtocol.py +++ b/lib/py/src/protocol/TProtocol.py @@ -1,3 +1,5 @@ +from thrift.Thrift import TException + class TType: STOP = 0 VOID = 1 @@ -21,6 +23,19 @@ class TMessageType: CALL = 1 REPLY = 2 +class TProtocolException(TException): + + """Custom Protocol Exception class""" + + UNKNOWN = 0 + INVALID_DATA = 1 + NEGATIVE_SIZE = 2 + SIZE_LIMIT = 3 + + def __init__(self, type=UNKNOWN, message=None): + TException.__init__(self, message) + self.type = type + class TProtocolBase: """Base class for Thrift protocol driver.""" diff --git a/lib/py/src/transport/TSocket.py b/lib/py/src/transport/TSocket.py index c8f9e362..ee429cd5 100644 --- a/lib/py/src/transport/TSocket.py +++ b/lib/py/src/transport/TSocket.py @@ -22,8 +22,11 @@ class TSocket(TTransportBase): self.handle.settimeout(ms/1000.00) def open(self): - self.handle = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - self.handle.connect((self.host, self.port)) + try: + self.handle = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.handle.connect((self.host, self.port)) + except socket.error, e: + raise TTransportException(TTransportException.NOT_OPEN, e.message) def close(self): if self.handle != None: @@ -42,7 +45,7 @@ class TSocket(TTransportBase): while sent < have: plus = self.handle.send(buff) if plus == 0: - raise TTransportException('sent 0 bytes') + raise TTransportException('TSocket sent 0 bytes') sent += plus buff = buff[plus:] diff --git a/lib/py/src/transport/TTransport.py b/lib/py/src/transport/TTransport.py index 502b3270..b8ca7b1e 100644 --- a/lib/py/src/transport/TTransport.py +++ b/lib/py/src/transport/TTransport.py @@ -1,11 +1,20 @@ from cStringIO import StringIO from struct import pack,unpack +from thrift.Thrift import TException -class TTransportException(Exception): +class TTransportException(TException): """Custom Transport Exception class""" - pass + UNKNOWN = 0, + NOT_OPEN = 1, + ALREADY_OPEN = 2, + TIMED_OUT = 3, + END_OF_FILE = 4, + + def __init__(self, type=UNKNOWN, message=None): + TException.__init__(self, message) + self.type = type class TTransportBase: -- 2.17.1