Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 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/ |
| 6 | |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 7 | #ifndef _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_ |
| 8 | #define _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_ 1 |
| 9 | |
| 10 | #include <boost/lexical_cast.hpp> |
| 11 | #include <string> |
| 12 | |
| 13 | namespace facebook { namespace thrift { namespace protocol { |
| 14 | |
| 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. |
| 21 | * |
| 22 | * @author Mark Slee <mcslee@facebook.com> |
| 23 | */ |
| 24 | class TProtocolException : public facebook::thrift::TException { |
| 25 | public: |
| 26 | |
| 27 | /** |
| 28 | * Error codes for the various types of exceptions. |
| 29 | */ |
| 30 | enum TProtocolExceptionType { |
| 31 | UNKNOWN = 0, |
| 32 | INVALID_DATA = 1, |
| 33 | NEGATIVE_SIZE = 2, |
Mark Slee | 808454e | 2007-06-20 21:51:57 +0000 | [diff] [blame] | 34 | SIZE_LIMIT = 3, |
| 35 | BAD_VERSION = 4, |
David Reiss | 00dcccf | 2007-07-21 01:18:10 +0000 | [diff] [blame] | 36 | NOT_IMPLEMENTED = 5, |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | TProtocolException() : |
| 40 | facebook::thrift::TException(), |
| 41 | type_(UNKNOWN) {} |
| 42 | |
| 43 | TProtocolException(TProtocolExceptionType type) : |
| 44 | facebook::thrift::TException(), |
| 45 | type_(type) {} |
| 46 | |
Mark Slee | 82a6c0f | 2007-04-04 21:08:21 +0000 | [diff] [blame] | 47 | TProtocolException(const std::string& message) : |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 48 | facebook::thrift::TException(message), |
| 49 | type_(UNKNOWN) {} |
| 50 | |
Mark Slee | 82a6c0f | 2007-04-04 21:08:21 +0000 | [diff] [blame] | 51 | TProtocolException(TProtocolExceptionType type, const std::string& message) : |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 52 | facebook::thrift::TException(message), |
| 53 | type_(type) {} |
| 54 | |
| 55 | virtual ~TProtocolException() throw() {} |
| 56 | |
| 57 | /** |
| 58 | * Returns an error code that provides information about the type of error |
| 59 | * that has occurred. |
| 60 | * |
| 61 | * @return Error code |
| 62 | */ |
| 63 | TProtocolExceptionType getType() { |
| 64 | return type_; |
| 65 | } |
| 66 | |
| 67 | virtual const char* what() const throw() { |
| 68 | if (message_.empty()) { |
| 69 | return (std::string("Default Protocol Exception: ") + |
| 70 | boost::lexical_cast<std::string>(type_)).c_str(); |
| 71 | } else { |
| 72 | return message_.c_str(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | protected: |
| 77 | /** |
| 78 | * Error code |
| 79 | */ |
| 80 | TProtocolExceptionType type_; |
| 81 | |
| 82 | }; |
| 83 | |
| 84 | }}} // facebook::thrift::protocol |
| 85 | |
| 86 | #endif // #ifndef _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_ |