blob: 10178e11a71062fd0f21f8e7a580a6227efd8b0e [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
Konrad Grochowski16a23a62014-11-13 15:33:38 +010025namespace apache {
26namespace thrift {
27namespace protocol {
Mark Sleef9831082007-02-20 20:59:21 +000028
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
David Reiss3bb5e052010-01-25 19:31:31 +000032 * wrapper around the vague UNIX E_ error codes that lets a common code
Mark Sleef9831082007-02-20 20:59:21 +000033 * base of error handling to be used for various types of protocols, i.e.
34 * pipes etc.
35 *
Mark Sleef9831082007-02-20 20:59:21 +000036 */
T Jake Lucianib5e62212009-01-31 22:36:20 +000037class TProtocolException : public apache::thrift::TException {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010038public:
Mark Sleef9831082007-02-20 20:59:21 +000039 /**
40 * Error codes for the various types of exceptions.
41 */
Konrad Grochowski16a23a62014-11-13 15:33:38 +010042 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
Mark Sleef9831082007-02-20 20:59:21 +000050 };
51
Konrad Grochowski16a23a62014-11-13 15:33:38 +010052 TProtocolException() : apache::thrift::TException(), type_(UNKNOWN) {}
Mark Sleef9831082007-02-20 20:59:21 +000053
Konrad Grochowski16a23a62014-11-13 15:33:38 +010054 TProtocolException(TProtocolExceptionType type) : apache::thrift::TException(), type_(type) {}
Mark Sleef9831082007-02-20 20:59:21 +000055
Konrad Grochowski16a23a62014-11-13 15:33:38 +010056 TProtocolException(const std::string& message)
57 : apache::thrift::TException(message), type_(UNKNOWN) {}
Mark Sleef9831082007-02-20 20:59:21 +000058
Konrad Grochowski16a23a62014-11-13 15:33:38 +010059 TProtocolException(TProtocolExceptionType type, const std::string& message)
60 : apache::thrift::TException(message), type_(type) {}
Mark Sleef9831082007-02-20 20:59:21 +000061
cyy7b935592019-01-05 10:04:25 +080062 virtual ~TProtocolException() noexcept {}
Mark Sleef9831082007-02-20 20:59:21 +000063
64 /**
65 * Returns an error code that provides information about the type of error
66 * that has occurred.
67 *
68 * @return Error code
69 */
tpcwangd42d8be2016-03-24 09:56:10 -070070 TProtocolExceptionType getType() const { return type_; }
Mark Sleef9831082007-02-20 20:59:21 +000071
cyy7b935592019-01-05 10:04:25 +080072 virtual const char* what() const noexcept {
Mark Sleef9831082007-02-20 20:59:21 +000073 if (message_.empty()) {
David Reissadad4ab2007-12-14 20:56:04 +000074 switch (type_) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010075 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)";
David Reissadad4ab2007-12-14 20:56:04 +000089 }
Mark Sleef9831082007-02-20 20:59:21 +000090 } else {
91 return message_.c_str();
92 }
93 }
94
Konrad Grochowski16a23a62014-11-13 15:33:38 +010095protected:
David Reiss0c90f6f2008-02-06 22:18:40 +000096 /**
Mark Sleef9831082007-02-20 20:59:21 +000097 * Error code
98 */
99 TProtocolExceptionType type_;
Mark Sleef9831082007-02-20 20:59:21 +0000100};
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100101}
102}
103} // apache::thrift::protocol
Mark Sleef9831082007-02-20 20:59:21 +0000104
105#endif // #ifndef _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_