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
diff --git a/lib/py/src/Thrift.py b/lib/py/src/Thrift.py
index 1be84e2..81689b7 100644
--- a/lib/py/src/Thrift.py
+++ b/lib/py/src/Thrift.py
@@ -4,3 +4,59 @@
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 15206b0..e35688e 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 @@
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 c8f9e36..ee429cd 100644
--- a/lib/py/src/transport/TSocket.py
+++ b/lib/py/src/transport/TSocket.py
@@ -22,8 +22,11 @@
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 @@
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 502b327..b8ca7b1 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: