r1403@opsdev009 (orig r74388): dreiss | 2007-12-14 12:56:04 -0800
[amiethrift.git] / lib / cpp / src / protocol / TProtocolException.h
blob4429a623a4575731804c40c2905dba6bc0ec86f8
1 // Copyright (c) 2006- Facebook
2 // Distributed under the Thrift Software License
3 //
4 // See accompanying file LICENSE or visit the Thrift site at:
5 // http://developers.facebook.com/thrift/
7 #ifndef _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_
8 #define _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_ 1
10 #include <boost/lexical_cast.hpp>
11 #include <string>
13 namespace facebook { namespace thrift { namespace protocol {
15 /**
16 * Class to encapsulate all the possible types of protocol errors that may
17 * occur in various protocol systems. This provides a sort of generic
18 * wrapper around the shitty UNIX E_ error codes that lets a common code
19 * base of error handling to be used for various types of protocols, i.e.
20 * pipes etc.
22 * @author Mark Slee <mcslee@facebook.com>
24 class TProtocolException : public facebook::thrift::TException {
25 public:
27 /**
28 * Error codes for the various types of exceptions.
30 enum TProtocolExceptionType {
31 UNKNOWN = 0,
32 INVALID_DATA = 1,
33 NEGATIVE_SIZE = 2,
34 SIZE_LIMIT = 3,
35 BAD_VERSION = 4,
36 NOT_IMPLEMENTED = 5,
39 TProtocolException() :
40 facebook::thrift::TException(),
41 type_(UNKNOWN) {}
43 TProtocolException(TProtocolExceptionType type) :
44 facebook::thrift::TException(),
45 type_(type) {}
47 TProtocolException(const std::string& message) :
48 facebook::thrift::TException(message),
49 type_(UNKNOWN) {}
51 TProtocolException(TProtocolExceptionType type, const std::string& message) :
52 facebook::thrift::TException(message),
53 type_(type) {}
55 virtual ~TProtocolException() throw() {}
57 /**
58 * Returns an error code that provides information about the type of error
59 * that has occurred.
61 * @return Error code
63 TProtocolExceptionType getType() {
64 return type_;
67 virtual const char* what() const throw() {
68 if (message_.empty()) {
69 switch (type_) {
70 case UNKNOWN : return "TProtocolException: Unknown protocol exception";
71 case INVALID_DATA : return "TProtocolException: Invalid data";
72 case NEGATIVE_SIZE : return "TProtocolException: Negative size";
73 case SIZE_LIMIT : return "TProtocolException: Exceeded size limit";
74 case BAD_VERSION : return "TProtocolException: Invalid version";
75 case NOT_IMPLEMENTED : return "TProtocolException: Not implemented";
76 default : return "TProtocolException: (Invalid exception type)";
78 } else {
79 return message_.c_str();
83 protected:
84 /**
85 * Error code
87 TProtocolExceptionType type_;
91 }}} // facebook::thrift::protocol
93 #endif // #ifndef _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_