David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 20 | #ifndef _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_ |
| 21 | #define _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_ 1 |
| 22 | |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 23 | #include <string> |
| 24 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 25 | namespace apache { namespace thrift { namespace protocol { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Class to encapsulate all the possible types of protocol errors that may |
| 29 | * occur in various protocol systems. This provides a sort of generic |
David Reiss | 3bb5e05 | 2010-01-25 19:31:31 +0000 | [diff] [blame] | 30 | * wrapper around the vague UNIX E_ error codes that lets a common code |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 31 | * base of error handling to be used for various types of protocols, i.e. |
| 32 | * pipes etc. |
| 33 | * |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 34 | */ |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 35 | class TProtocolException : public apache::thrift::TException { |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 36 | public: |
| 37 | |
| 38 | /** |
| 39 | * Error codes for the various types of exceptions. |
| 40 | */ |
David Reiss | 322e595 | 2008-12-05 02:54:09 +0000 | [diff] [blame] | 41 | enum TProtocolExceptionType |
| 42 | { UNKNOWN = 0 |
| 43 | , INVALID_DATA = 1 |
| 44 | , NEGATIVE_SIZE = 2 |
| 45 | , SIZE_LIMIT = 3 |
| 46 | , BAD_VERSION = 4 |
| 47 | , NOT_IMPLEMENTED = 5 |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | TProtocolException() : |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 51 | apache::thrift::TException(), |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 52 | type_(UNKNOWN) {} |
| 53 | |
| 54 | TProtocolException(TProtocolExceptionType type) : |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 55 | apache::thrift::TException(), |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 56 | type_(type) {} |
| 57 | |
Mark Slee | 82a6c0f | 2007-04-04 21:08:21 +0000 | [diff] [blame] | 58 | TProtocolException(const std::string& message) : |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 59 | apache::thrift::TException(message), |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 60 | type_(UNKNOWN) {} |
| 61 | |
Mark Slee | 82a6c0f | 2007-04-04 21:08:21 +0000 | [diff] [blame] | 62 | TProtocolException(TProtocolExceptionType type, const std::string& message) : |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 63 | apache::thrift::TException(message), |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 64 | type_(type) {} |
| 65 | |
| 66 | virtual ~TProtocolException() throw() {} |
| 67 | |
| 68 | /** |
| 69 | * Returns an error code that provides information about the type of error |
| 70 | * that has occurred. |
| 71 | * |
| 72 | * @return Error code |
| 73 | */ |
| 74 | TProtocolExceptionType getType() { |
| 75 | return type_; |
| 76 | } |
| 77 | |
| 78 | virtual const char* what() const throw() { |
| 79 | if (message_.empty()) { |
David Reiss | adad4ab | 2007-12-14 20:56:04 +0000 | [diff] [blame] | 80 | switch (type_) { |
| 81 | case UNKNOWN : return "TProtocolException: Unknown protocol exception"; |
| 82 | case INVALID_DATA : return "TProtocolException: Invalid data"; |
| 83 | case NEGATIVE_SIZE : return "TProtocolException: Negative size"; |
| 84 | case SIZE_LIMIT : return "TProtocolException: Exceeded size limit"; |
| 85 | case BAD_VERSION : return "TProtocolException: Invalid version"; |
| 86 | case NOT_IMPLEMENTED : return "TProtocolException: Not implemented"; |
| 87 | default : return "TProtocolException: (Invalid exception type)"; |
| 88 | } |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 89 | } else { |
| 90 | return message_.c_str(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | protected: |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 95 | /** |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 96 | * Error code |
| 97 | */ |
| 98 | TProtocolExceptionType type_; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 99 | |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 100 | }; |
| 101 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 102 | }}} // apache::thrift::protocol |
Mark Slee | f983108 | 2007-02-20 20:59:21 +0000 | [diff] [blame] | 103 | |
| 104 | #endif // #ifndef _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_ |