Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame^] | 1 | #ifndef _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_ |
| 2 | #define _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_ 1 |
| 3 | |
| 4 | #include <boost/lexical_cast.hpp> |
| 5 | #include <string> |
| 6 | |
| 7 | namespace facebook { namespace thrift { namespace protocol { |
| 8 | |
| 9 | /** |
| 10 | * Class to encapsulate all the possible types of protocol errors that may |
| 11 | * occur in various protocol systems. This provides a sort of generic |
| 12 | * wrapper around the shitty UNIX E_ error codes that lets a common code |
| 13 | * base of error handling to be used for various types of protocols, i.e. |
| 14 | * pipes etc. |
| 15 | * |
| 16 | * @author Mark Slee <mcslee@facebook.com> |
| 17 | */ |
| 18 | class TProtocolException : public facebook::thrift::TException { |
| 19 | public: |
| 20 | |
| 21 | /** |
| 22 | * Error codes for the various types of exceptions. |
| 23 | */ |
| 24 | enum TProtocolExceptionType { |
| 25 | UNKNOWN = 0, |
| 26 | INVALID_DATA = 1, |
| 27 | NEGATIVE_SIZE = 2, |
| 28 | SIZE_LIMIT = 3 |
| 29 | }; |
| 30 | |
| 31 | TProtocolException() : |
| 32 | facebook::thrift::TException(), |
| 33 | type_(UNKNOWN) {} |
| 34 | |
| 35 | TProtocolException(TProtocolExceptionType type) : |
| 36 | facebook::thrift::TException(), |
| 37 | type_(type) {} |
| 38 | |
| 39 | TProtocolException(const std::string message) : |
| 40 | facebook::thrift::TException(message), |
| 41 | type_(UNKNOWN) {} |
| 42 | |
| 43 | TProtocolException(TProtocolExceptionType type, const std::string message) : |
| 44 | facebook::thrift::TException(message), |
| 45 | type_(type) {} |
| 46 | |
| 47 | virtual ~TProtocolException() throw() {} |
| 48 | |
| 49 | /** |
| 50 | * Returns an error code that provides information about the type of error |
| 51 | * that has occurred. |
| 52 | * |
| 53 | * @return Error code |
| 54 | */ |
| 55 | TProtocolExceptionType getType() { |
| 56 | return type_; |
| 57 | } |
| 58 | |
| 59 | virtual const char* what() const throw() { |
| 60 | if (message_.empty()) { |
| 61 | return (std::string("Default Protocol Exception: ") + |
| 62 | boost::lexical_cast<std::string>(type_)).c_str(); |
| 63 | } else { |
| 64 | return message_.c_str(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | protected: |
| 69 | /** |
| 70 | * Error code |
| 71 | */ |
| 72 | TProtocolExceptionType type_; |
| 73 | |
| 74 | }; |
| 75 | |
| 76 | }}} // facebook::thrift::protocol |
| 77 | |
| 78 | #endif // #ifndef _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_ |