blob: 4ddb81e072d0ff2769de8b59fdaa7b5d42e36b1e [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
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 Slee9f0c6512007-02-28 23:58:26 +000019
Mark Sleef9831082007-02-20 20:59:21 +000020#ifndef _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_
21#define _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_ 1
22
Mark Sleef9831082007-02-20 20:59:21 +000023#include <string>
24
T Jake Lucianib5e62212009-01-31 22:36:20 +000025namespace apache { namespace thrift { namespace protocol {
Mark Sleef9831082007-02-20 20:59:21 +000026
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 Reiss3bb5e052010-01-25 19:31:31 +000030 * wrapper around the vague UNIX E_ error codes that lets a common code
Mark Sleef9831082007-02-20 20:59:21 +000031 * base of error handling to be used for various types of protocols, i.e.
32 * pipes etc.
33 *
Mark Sleef9831082007-02-20 20:59:21 +000034 */
T Jake Lucianib5e62212009-01-31 22:36:20 +000035class TProtocolException : public apache::thrift::TException {
Mark Sleef9831082007-02-20 20:59:21 +000036 public:
37
38 /**
39 * Error codes for the various types of exceptions.
40 */
David Reiss322e5952008-12-05 02:54:09 +000041 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
Jens Geyer885c6792014-05-02 21:31:55 +020048 , DEPTH_LIMIT = 6
Mark Sleef9831082007-02-20 20:59:21 +000049 };
50
51 TProtocolException() :
T Jake Lucianib5e62212009-01-31 22:36:20 +000052 apache::thrift::TException(),
Mark Sleef9831082007-02-20 20:59:21 +000053 type_(UNKNOWN) {}
54
55 TProtocolException(TProtocolExceptionType type) :
T Jake Lucianib5e62212009-01-31 22:36:20 +000056 apache::thrift::TException(),
Mark Sleef9831082007-02-20 20:59:21 +000057 type_(type) {}
58
Mark Slee82a6c0f2007-04-04 21:08:21 +000059 TProtocolException(const std::string& message) :
T Jake Lucianib5e62212009-01-31 22:36:20 +000060 apache::thrift::TException(message),
Mark Sleef9831082007-02-20 20:59:21 +000061 type_(UNKNOWN) {}
62
Mark Slee82a6c0f2007-04-04 21:08:21 +000063 TProtocolException(TProtocolExceptionType type, const std::string& message) :
T Jake Lucianib5e62212009-01-31 22:36:20 +000064 apache::thrift::TException(message),
Mark Sleef9831082007-02-20 20:59:21 +000065 type_(type) {}
66
67 virtual ~TProtocolException() throw() {}
68
69 /**
70 * Returns an error code that provides information about the type of error
71 * that has occurred.
72 *
73 * @return Error code
74 */
75 TProtocolExceptionType getType() {
76 return type_;
77 }
78
79 virtual const char* what() const throw() {
80 if (message_.empty()) {
David Reissadad4ab2007-12-14 20:56:04 +000081 switch (type_) {
82 case UNKNOWN : return "TProtocolException: Unknown protocol exception";
83 case INVALID_DATA : return "TProtocolException: Invalid data";
84 case NEGATIVE_SIZE : return "TProtocolException: Negative size";
85 case SIZE_LIMIT : return "TProtocolException: Exceeded size limit";
86 case BAD_VERSION : return "TProtocolException: Invalid version";
87 case NOT_IMPLEMENTED : return "TProtocolException: Not implemented";
88 default : return "TProtocolException: (Invalid exception type)";
89 }
Mark Sleef9831082007-02-20 20:59:21 +000090 } else {
91 return message_.c_str();
92 }
93 }
94
95 protected:
David Reiss0c90f6f2008-02-06 22:18:40 +000096 /**
Mark Sleef9831082007-02-20 20:59:21 +000097 * Error code
98 */
99 TProtocolExceptionType type_;
David Reiss0c90f6f2008-02-06 22:18:40 +0000100
Mark Sleef9831082007-02-20 20:59:21 +0000101};
102
T Jake Lucianib5e62212009-01-31 22:36:20 +0000103}}} // apache::thrift::protocol
Mark Sleef9831082007-02-20 20:59:21 +0000104
105#endif // #ifndef _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_