blob: 33011b379f17933d0bbd76dc2f10d4c671db3276 [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
30 * wrapper around the shitty UNIX E_ error codes that lets a common code
31 * 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
Mark Sleef9831082007-02-20 20:59:21 +000048 };
49
50 TProtocolException() :
T Jake Lucianib5e62212009-01-31 22:36:20 +000051 apache::thrift::TException(),
Mark Sleef9831082007-02-20 20:59:21 +000052 type_(UNKNOWN) {}
53
54 TProtocolException(TProtocolExceptionType type) :
T Jake Lucianib5e62212009-01-31 22:36:20 +000055 apache::thrift::TException(),
Mark Sleef9831082007-02-20 20:59:21 +000056 type_(type) {}
57
Mark Slee82a6c0f2007-04-04 21:08:21 +000058 TProtocolException(const std::string& message) :
T Jake Lucianib5e62212009-01-31 22:36:20 +000059 apache::thrift::TException(message),
Mark Sleef9831082007-02-20 20:59:21 +000060 type_(UNKNOWN) {}
61
Mark Slee82a6c0f2007-04-04 21:08:21 +000062 TProtocolException(TProtocolExceptionType type, const std::string& message) :
T Jake Lucianib5e62212009-01-31 22:36:20 +000063 apache::thrift::TException(message),
Mark Sleef9831082007-02-20 20:59:21 +000064 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 Reissadad4ab2007-12-14 20:56:04 +000080 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 Sleef9831082007-02-20 20:59:21 +000089 } else {
90 return message_.c_str();
91 }
92 }
93
94 protected:
David Reiss0c90f6f2008-02-06 22:18:40 +000095 /**
Mark Sleef9831082007-02-20 20:59:21 +000096 * Error code
97 */
98 TProtocolExceptionType type_;
David Reiss0c90f6f2008-02-06 22:18:40 +000099
Mark Sleef9831082007-02-20 20:59:21 +0000100};
101
T Jake Lucianib5e62212009-01-31 22:36:20 +0000102}}} // apache::thrift::protocol
Mark Sleef9831082007-02-20 20:59:21 +0000103
104#endif // #ifndef _THRIFT_PROTOCOL_TPROTOCOLEXCEPTION_H_