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 | |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 10 | #include <string> |
| 11 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 12 | namespace apache { namespace thrift { namespace protocol { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 13 | |
| 14 | /** |
| 15 | * Class to encapsulate all the possible types of protocol errors that may |
| 16 | * occur in various protocol systems. This provides a sort of generic |
| 17 | * wrapper around the shitty UNIX E_ error codes that lets a common code |
| 18 | * base of error handling to be used for various types of protocols, i.e. |
| 19 | * pipes etc. |
| 20 | * |
| 21 | * @author Mark Slee <mcslee@facebook.com> |
| 22 | */ |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 23 | class TProtocolException : public apache::thrift::TException { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 24 | public: |
| 25 | |
| 26 | /** |
| 27 | * Error codes for the various types of exceptions. |
| 28 | */ |
David Reiss | 322e595 | 2008-12-05 02:54:09 +0000 | [diff] [blame] | 29 | enum TProtocolExceptionType |
| 30 | { UNKNOWN = 0 |
| 31 | , INVALID_DATA = 1 |
| 32 | , NEGATIVE_SIZE = 2 |
| 33 | , SIZE_LIMIT = 3 |
| 34 | , BAD_VERSION = 4 |
| 35 | , NOT_IMPLEMENTED = 5 |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | TProtocolException() : |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 39 | apache::thrift::TException(), |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 40 | type_(UNKNOWN) {} |
| 41 | |
| 42 | TProtocolException(TProtocolExceptionType type) : |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 43 | apache::thrift::TException(), |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 44 | type_(type) {} |
| 45 | |
Mark Slee | 82a6c0f | 2007-04-04 21:08:21 +0000 | [diff] [blame] | 46 | TProtocolException(const std::string& message) : |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 47 | apache::thrift::TException(message), |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 48 | type_(UNKNOWN) {} |
| 49 | |
Mark Slee | 82a6c0f | 2007-04-04 21:08:21 +0000 | [diff] [blame] | 50 | TProtocolException(TProtocolExceptionType type, const std::string& message) : |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 51 | apache::thrift::TException(message), |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 52 | type_(type) {} |
| 53 | |
| 54 | virtual ~TProtocolException() throw() {} |
| 55 | |
| 56 | /** |
| 57 | * Returns an error code that provides information about the type of error |
| 58 | * that has occurred. |
| 59 | * |
| 60 | * @return Error code |
| 61 | */ |
| 62 | TProtocolExceptionType getType() { |
| 63 | return type_; |
| 64 | } |
| 65 | |
| 66 | virtual const char* what() const throw() { |
| 67 | if (message_.empty()) { |
David Reiss | adad4ab | 2007-12-14 20:56:04 +0000 | [diff] [blame] | 68 | switch (type_) { |
| 69 | case UNKNOWN : return "TProtocolException: Unknown protocol exception"; |
| 70 | case INVALID_DATA : return "TProtocolException: Invalid data"; |
| 71 | case NEGATIVE_SIZE : return "TProtocolException: Negative size"; |
| 72 | case SIZE_LIMIT : return "TProtocolException: Exceeded size limit"; |
| 73 | case BAD_VERSION : return "TProtocolException: Invalid version"; |
| 74 | case NOT_IMPLEMENTED : return "TProtocolException: Not implemented"; |
| 75 | default : return "TProtocolException: (Invalid exception type)"; |
| 76 | } |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 77 | } else { |
| 78 | return message_.c_str(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | protected: |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 83 | /** |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 84 | * Error code |
| 85 | */ |
| 86 | TProtocolExceptionType type_; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 87 | |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 90 | }}} // apache::thrift::protocol |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 91 | |
| 92 | #endif // #ifndef _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_ |