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 | */ |
19 | |
20 | #ifndef _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_ |
21 | #define _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_ 1 |
22 | |
23 | #include <string> |
24 | |
25 | namespace apache { |
26 | namespace thrift { |
27 | namespace protocol { |
28 | |
29 | /** |
30 | * Class to encapsulate all the possible types of protocol errors that may |
31 | * occur in various protocol systems. This provides a sort of generic |
32 | * wrapper around the vague UNIX E_ error codes that lets a common code |
33 | * base of error handling to be used for various types of protocols, i.e. |
34 | * pipes etc. |
35 | * |
36 | */ |
37 | class TProtocolException : public apache::thrift::TException { |
38 | public: |
39 | /** |
40 | * Error codes for the various types of exceptions. |
41 | */ |
42 | enum TProtocolExceptionType { |
43 | UNKNOWN = 0, |
44 | INVALID_DATA = 1, |
45 | NEGATIVE_SIZE = 2, |
46 | SIZE_LIMIT = 3, |
47 | BAD_VERSION = 4, |
48 | NOT_IMPLEMENTED = 5, |
49 | DEPTH_LIMIT = 6 |
50 | }; |
51 | |
52 | TProtocolException() : apache::thrift::TException(), type_(UNKNOWN) {} |
53 | |
54 | TProtocolException(TProtocolExceptionType type) : apache::thrift::TException(), type_(type) {} |
55 | |
56 | TProtocolException(const std::string& message) |
57 | : apache::thrift::TException(message), type_(UNKNOWN) {} |
58 | |
59 | TProtocolException(TProtocolExceptionType type, const std::string& message) |
60 | : apache::thrift::TException(message), type_(type) {} |
61 | |
62 | virtual ~TProtocolException() throw() {} |
63 | |
64 | /** |
65 | * Returns an error code that provides information about the type of error |
66 | * that has occurred. |
67 | * |
68 | * @return Error code |
69 | */ |
70 | TProtocolExceptionType getType() const { return type_; } |
71 | |
72 | virtual const char* what() const throw() { |
73 | if (message_.empty()) { |
74 | switch (type_) { |
75 | case UNKNOWN: |
76 | return "TProtocolException: Unknown protocol exception" ; |
77 | case INVALID_DATA: |
78 | return "TProtocolException: Invalid data" ; |
79 | case NEGATIVE_SIZE: |
80 | return "TProtocolException: Negative size" ; |
81 | case SIZE_LIMIT: |
82 | return "TProtocolException: Exceeded size limit" ; |
83 | case BAD_VERSION: |
84 | return "TProtocolException: Invalid version" ; |
85 | case NOT_IMPLEMENTED: |
86 | return "TProtocolException: Not implemented" ; |
87 | default: |
88 | return "TProtocolException: (Invalid exception type)" ; |
89 | } |
90 | } else { |
91 | return message_.c_str(); |
92 | } |
93 | } |
94 | |
95 | protected: |
96 | /** |
97 | * Error code |
98 | */ |
99 | TProtocolExceptionType type_; |
100 | }; |
101 | } |
102 | } |
103 | } // apache::thrift::protocol |
104 | |
105 | #endif // #ifndef _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_ |
106 | |